栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android:具有现有布局的自定义视图的多个视图子级

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android:具有现有布局的自定义视图的多个视图子级

绝对有可能并且鼓励创建自定义容器视图。这就是Android所谓的复合控件。所以:

public class MyCustomView extends RelativeLayout {    private LinearLayout mContentView;    public MyCustomView(Context context) {        this(context, null);    }    public MyCustomView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        //Inflate and attach your child XML        LayoutInflater.from(context).inflate(R.layout.custom_layout, this);        //Get a reference to the layout where you want children to be placed        mContentView = (LinearLayout) findViewById(R.id.content);        //Do any more custom init you would like to access children and do setup    }    @Override    public void addView(View child, int index, ViewGroup.LayoutParams params) {        if(mContentView == null){ super.addView(child, index, params);        } else { //Forward these calls to the content view mContentView.addView(child, index, params);        }    }}

您可以根据需要覆盖尽可能多的版本

addView()
,但最后它们都会回调到我在样本中放置的版本。仅重写此方法将使框架将在XML标记内找到的所有子代传递给特定的子代容器。

然后像这样修改XML:

RES /布局/custom_layout.xml

<merge>  <SomeView />  <SomeOtherView />  <!-- maybe more layout stuff here later -->  <LinearLayout      android:id="@+id/content" /></merge>

使用的原因

<merge>
是简化层次结构。所有子视图都将附加到您的自定义类(即)上
RelativeLayout
。如果不使用
<merge>
,则最终会在所有子项上
RelativeLayout
附加一个
RelativeLayout
附加项,这可能会引起问题。

高温超导



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号