代码之家  ›  专栏  ›  技术社区  ›  justinl

在Android运行时是否可以交换XML布局?

  •  5
  • justinl  · 技术社区  · 16 年前

    谢谢!

    3 回复  |  直到 16 年前
        1
  •  13
  •   Joe    12 年前

    android:visibility="gone" 在XML中,并在运行时调用 hiddenView.setVisibility(View.VISIBLE);

    您的XML将如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <LinearLayout android:id="@+id/layout1"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
        </LinearLayout>
        <LinearLayout android:id="@+id/layout2"
            android:visibility="gone"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
        </LinearLayout>
    </RelativeLayout>
    

    layout1.setVisibility(View.GONE);
    layout2.setVisibility(View.VISIBLE);
    

    这实际上会创建布局交换的外观。

    我相信如果在两个布局之间共享对象,这也会起作用。一定要先用 android:id="@+id/view1" android:id="@id/view1" (不带+)。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <LinearLayout android:id="@+id/layout1"
            android:layout_width="fill_parent">
            <View android:id="@+id/view1"/>
        </LinearLayout>
        <LinearLayout android:id="@+id/layout2"
            android:visibility="gone"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
             <View android:id="@id/view1"/>
        </LinearLayout>
    </RelativeLayout>
    
        2
  •  1
  •   Sephy    16 年前

    你不能创建一个新的活动吗?否则,我认为您可能需要考虑从主容器中删除视图并通过代码重建UI。如果我记得很清楚,每个活动只能调用一次setContentView。。。

        3
  •  1
  •   EboMike    16 年前

    getLayoutInflater().inflate(resourceId, rootViewGroup)