how to add views inside a custom View?

2019-06-16 05:10发布

I have a class like that, and there are about 10 of them

public class DataItemPlainView extends View{

    public DataItemPlainView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }}

Now I need to put TextView, ImageView etc inside this view. And when I call it from somewhere, I want to get my customView. setting a view to a custom layout is a case too.

Thanks

2条回答
虎瘦雄心在
2楼-- · 2019-06-16 05:49

I would try extending some kind of Layout. Remember that (for the most part) they are also treated as Views. For more information/deciding which Layout to pick, try looking here:

http://developer.android.com/guide/topics/ui/layout-objects.html

查看更多
萌系小妹纸
3楼-- · 2019-06-16 05:53

Your custom view needs to extend ViewGroup or one of the other classes that extends ViewGroup. For example, you could extend from RelativeLayout or LinearLayout if those layouts fits what your custom view needs to do.

Remember, even the layout classes are just another View. They just happen to have methods to add other views as children and have code to recursively measure and draw their children.

查看更多
登录 后发表回答