如何在自定义视图中添加视图? [英] how to add views inside a custom View?

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

问题描述

我有一个这样的课程,大约有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
    }}

现在,我需要在此视图中放置TextView,ImageView等.当我从某个地方调用它时,我想获取我的customView.将视图设置为自定义布局也是一种情况.

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.

谢谢

推荐答案

您的自定义视图需要扩展ViewGroup或扩展ViewGroup的其他类之一.例如,如果这些布局适合您的自定义视图的需要,则可以从RelativeLayoutLinearLayout扩展.

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.

请记住,甚至布局类也只是另一个View.他们只是碰巧拥有添加其他视图作为子视图的方法,并且具有递归地度量和绘制其子视图的代码.

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.

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

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