开发者

how to to create custom & composite view in android

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 ima开发者_开发技巧ges. 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:

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

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);
  ...
 }
 ...
}

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


you might want to read about the extended class' constructors here: Do I need all three constructors for an Android custom view?


You should read the documentation here to get started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜