简体   繁体   English

重用Android布局

[英]Re-use Android layout

i have an root_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>

Now i want, everytime i want to change the screen, that these layout is used and filled with an other declared layout.xml 现在,我想每次更改屏幕时都使用这些布局,并用另一个声明的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. 例如,它的文件名为root_layout.xml。

Then, if you want to use it in another view, simply add this to your xml file: 然后,如果要在其他视图中使用它,只需将其添加到xml文件中即可:

<include layout="@layout/root_layout"></include>

And root_layout will be inflated into another layout. root_layout将被夸大为另一个布局。

Notice that every attribute of root_layout is preserved. 请注意,保留了root_layout的每个属性。 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() . 您没有说它是如何工作的,所以我假设您在屏幕上看到的都是两个视图……您应该使用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: 如果要向发布的布局中添加不同的布局到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.

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