Reputation: 1157
i have an 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>
Now i want, everytime i want to change the screen, that these layout is used and filled with an other declared layout.xml
I tried to use
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;
}
But this didn't work fine.Did anyone knows a nicer implementation?
Upvotes: 0
Views: 146
Reputation: 87064
If you want to add a different layout to the FrameLayout
from the layout that you posted, this is how you should do it:
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;
}
Upvotes: 1
Reputation: 16393
You didn't say how it wasn't working, so I'll assume you are left with both views on the screen... you should use removeAllViews()
.
Try this:
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;
}
Upvotes: 1
Reputation: 721
You can reuse the layout like this:
First, implement a layout like described in your problem. For example, it's file name is root_layout.xml.
Then, if you want to use it in another view, simply add this to your xml file:
<include layout="@layout/root_layout"></include>
And root_layout will be inflated into another layout.
Notice that every attribute of root_layout is preserved. So pay attention on them.
Upvotes: 1