如何创建自定义&Android中的复合视图 [英] how to to create custom & composite view in android

查看:58
本文介绍了如何创建自定义&Android中的复合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android中创建一个自定义和复合视图.我的自定义视图将包括1个textview,5个单选按钮,两个按钮和一些图像.我不知道该怎么做.

I would like to create a custom and composite view in android. My custom view will include 1 textview, 5 radiobuttons two buttons and some images. I don't know how to do it. if there would be some example or code spinet, it would be nice..

推荐答案

我认为它可以为您提供帮助:

I think it could help you:

首先,您可以在xml中定义一个RelativeLayout,将所有需要的元素放在您想要的位置.

First you can define a RelativeLayout in xml, with all the elements you want on it, placed like you want.

第二,当您定义了该布局时,您可以开发一个自定义类,扩展RelativeLayout,然后在该类的构造函数方法中将该布局膨胀,如下所示:

Second, when you have that layout defined, you can develop a custom class, extending RelativeLayout, and inflate that layout in the constructor method of the class, something like this:

public class MyCustomView extends RelativeLayout {

 ...

 public MyCustomView(Context context) {

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  addView(inflater.inflate(R.layout.your_layout, null));

  oneTextView = (TextView) findViewById(R.id.oneTextView);
  oneRadioButton = (RadioButton) findViewById(R.id.oneRadioButton);
  ...
 }
 ...
}

这时,您可以按常规方式在您的类中使用oneTextView,oneRadioButton等.

At this point, you can use oneTextView, oneRadioButton, etc., in your class in a normal way.

这篇关于如何创建自定义&Android中的复合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆