有没有快速的方法在相对布局之间切换?

时间:2016-07-17 05:32:51

标签: android android-layout

假设我有一个具有这种结构的XML:

FrameLayout
    RelativeLayout (on whole screen)
        Buttons, Texts etc.
    RelativeLayout (on whole screen)
        Buttons, Texts etc.

在“设计”或“预览”标签上,我总是看到我创建的第一个相对布局。我一直在通过使另一个看不见而在它们之间切换但是当你有两个以上的布局时它就是一团糟。 Android中是否有提供此功能的选项,我的意思是在一个XML中切换不同的布局?

2 个答案:

答案 0 :(得分:1)

在Android Studio中,如果它们同时填满您的屏幕,则无法同时正确查看两个RelativeLayouts。我建议你使用两个包含RelativeLayout的xml来根据你的需要进行设计。之后,您可以轻松地将它们合并为一个xml。例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <include android:layout_width="fill_parent" android:layout_height="fill_parent" layout="@layout/your_first_relativelayout" />
    <include android:layout_width="fill_parent" android:layout_height="fill_parent" layout="@layout/your_second_relativelayout" />
</FrameLayout>

如果要以编程方式隐藏或显示布局,则应为RelativeLayouts指定一个唯一ID,如下所示:

your_first_relativelayout.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/your_relative_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        ....
    </RelativeLayout>

要表明:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.your_relative_layout_id);
if (rl != null) {
    rl.setVisibility(View.VISIBLE);
}

隐藏它:

rl.setVisibility(View.GONE);

答案 1 :(得分:0)

我认为没有选择 最好使用两个布局文件并在编辑器中打开它们 (并使用<include>标签

将两个布局文件包含在原始布局文件中)