【问题标题】:Android: dynamic Layout change in DialogAndroid:Dialog中的动态布局更改
【发布时间】:2012-01-14 17:56:33
【问题描述】:

我想以编程方式更改对话框的布局:更改根 LinearLayout 方向。此根 LinearLayout 包含两个布局:GT 和 DI。 如果我使用 AlertDialog 效果很好,但如果我直接使用 Dialog 则效果不佳(使用带有自定义样式的 Dialog,因为我不想要任何标题或标题空间)。

这是我的 XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/GT_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GT"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup
        android:id="@+id/GT_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:height="5dp"
            android:text="GT #1" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:height="5dp"
            android:text="GT #2" />

    </RadioGroup>
</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DI_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DI"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbarAlwaysDrawVerticalTrack="true" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <RadioGroup
                android:id="@+id/DI_group"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RadioButton
                    android:id="@+id/DI_rdb_1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="DI #1" />

                <RadioButton
                    android:id="@+id/DI_rdb_2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="DI #2" />
            </RadioGroup>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

这是我的代码:

LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootLayout = inflater.inflate(R.layout.new_game, (ViewGroup) activity.findViewById(R.id.root_container));

if (screenIsLandscape) {
   ((LinearLayout) rootLayout).setOrientation(LinearLayout.HORIZONTAL);
} else {
   ((LinearLayout) rootLayout).setOrientation(LinearLayout.VERTICAL);
}

Dialog dlg = new Dialog(activity, R.style.FullHeightDialog);
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dlg.setContentView(R.layout.my_layout);
dlg.show();

我不想使用“layout-land”来避免重复 XML(我的 XML 相当大,但在本文中进行了简化)。 提前谢谢你。

【问题讨论】:

  • 您是否尝试过更改 setContentView() 以在 2 之间切换?
  • 谢谢。抱歉,但我不明白:在什么和什么之间切换?在 setContentView() 中,我目前使用“R.layout.my_layout”,这是我唯一的 XML 资源布局文件的名称。如果您的意思是在 GT 和 DI 之间切换,那不是我想要的:我想将它们从水平布局到垂直,反之亦然

标签: android layout dynamic dialog


【解决方案1】:

您从 xml 文件创建了新对象“rootLayout”,然后对其进行了更改(rootLayout,不是 xml)。现在此对象未与您的文件链接。

你有一个改变的对象,使用它:

dlg.setContentView(rootLayout);

【讨论】:

    猜你喜欢
    • 2015-08-30
    • 2015-06-26
    • 1970-01-01
    • 2012-12-20
    • 2021-12-22
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多