简体   繁体   中英

Re-use Android layout

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?

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.

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; 
} 

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM