[英]Re-use Android layout
我有一个root_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
android:weightSum="10">
<FrameLayout
android:id="@+id/parentViewHolder"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="8"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent" >
</FrameLayout>
<TextView
android:id="@+id/spaceHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="TEXT">
</TextView>
现在,我想每次更改屏幕时都使用这些布局,并用另一个声明的layout.xml填充
我尝试使用
protected void Setup(int _layoutResourceId)
{
layoutResourceId = _layoutResourceId;
FrameLayout holder = (FrameLayout) activity.findViewById(R.id.parentViewHolder);
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder.addView(layoutInflater.inflate(layoutResourceId, null, false));
activity.screen = this;
}
但这不能正常工作,有人知道更好的实现吗?
您可以像这样重复使用布局:
首先,实施问题所描述的布局。 例如,它的文件名为root_layout.xml。
然后,如果要在其他视图中使用它,只需将其添加到xml文件中即可:
<include layout="@layout/root_layout"></include>
root_layout将被夸大为另一个布局。
请注意,保留了root_layout的每个属性。 因此,请注意它们。
您没有说它是如何工作的,所以我假设您在屏幕上看到的都是两个视图……您应该使用removeAllViews()
。
尝试这个:
protected void Setup(int _layoutResourceId)
{
layoutResourceId = _layoutResourceId;
FrameLayout holder = (FrameLayout) activity.findViewById(R.id.parentViewHolder);
holder.removeAllViews();
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
holder.addView(layoutInflater.inflate(layoutResourceId, null, false));
activity.screen = this;
}
如果要向发布的布局中添加不同的布局到FrameLayout
,则应该这样做:
protected void Setup(int _layoutResourceId) {
layoutResourceId = _layoutResourceId;
FrameLayout holder = (FrameLayout) activity.findViewById(R.id.parentViewHolder);
holder.removeAllViews();
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(layoutResourceId, holder, true));
activity.screen = this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.