我想创建一个自定义布局以减少代码中的冗余。目前每个布局文件都有大约30行相同的代码。复合布局
我的目标是创建一个可以容纳孩子的自定义布局/视图。
<BaseLayout xmlns:...>
<!-- Normal Content -->
<Button />
<Label />
</BaseLayout>
虽然上述XML保持大部分内容,所述的baselayout本身含有其他观点和功能的XML:
<FrameLayout xmlns:...>
<LinearLayout><!-- contains the Header--></LinearLayout>
<LinearLayout><!-- INDIVIDUAL CONTENT HERE--></LinearLayout>
<FrameLayout><!-- contains the loading screen overlay --></FrameLayout>
</FrameLayout>
因此,从上面的XML所有儿童应插入到第二线性-布局。我已经成功地这样做了。但我面对的布局问题(匹配父母不匹配的父母,只包)
我的做法是用下面的逻辑扩展的LinearLayout:
/**
* extracting all children and adding them to the inflated base-layout
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = LayoutInflater.from(getContext()).inflate(R.layout.base_layout, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.base_layout_children);
while(0 < getChildCount())
{
View child = getChildAt(0);
LinearLayout.MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
removeViewAt(0);
linearLayout.addView(child, layoutParams);
}
this.addView(view);
}
是否有胶囊更好,更简洁的方法该XML和重用基础布局?我该如何解决match_parent问题?