首页 > 解决方案 > 横向 - 如何保留数据和布局属性?

问题描述

你好 Stack Overflow 社区,

每次我将方向从纵向更改为横向时,我一直试图让我的设备阻止活动擦除数据。

我发现我可以通过android:configChanges="orientation"在我的活动字段中添加来做到这一点,AndroidManifest.xml如下所示:

<activity android:name=".MainActivity"  android:configChanges="orientation|screenLayout|screenSize" >
...
</activity>

但是,这样做并没有考虑我在我的/res/layout-land/main.activity.xml

如果我删除所做的android:configChanges修改,它将应用所做的所有更改res/layout-land/activity_main.xml

我想澄清一下:

  1. 如果我使用,为什么布局没有被读取android:configChanges
  2. 有没有办法可以使方向不删除在保持布局属性的同时创建的数据?

我的横向视图的完整 XML 代码如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="80dp">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_team_a"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneTeamA"
                        android:text="@string/bt_score_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_fouls"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a_foul"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneFoulTeamA"
                        android:text="@string/bt_foul_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_penalty"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a_penalty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOnePenaltyTeamA"
                        android:text="@string/bt_penalty_plus_1" />


                </LinearLayout>

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:background="@android:color/darker_gray" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_team_b"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneTeamB"
                        android:text="@string/bt_score_plus_1" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_fouls"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b_foul"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneFoulTeamB"
                        android:text="@string/bt_foul_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_penalty"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b_penalty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOnePenaltyTeamB"
                        android:text="@string/bt_penalty_plus_1" />

                </LinearLayout>


            </LinearLayout>


            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="8dp"
                android:onClick="resetDetails"
                android:text="@string/bt_reset" />


        </RelativeLayout>

    </LinearLayout>

</ScrollView>

编辑

如果可能,希望有一个 XML 解决方案,而无需修补 Java 代码。

标签: androidxmllayoutorientation

解决方案


您可以使用savedInstanceState或 android 引入了ViewModel来以生命周期意识的方式管理 UI 相关数据。

了解更多信息 Android ViewModel

也可以参考这个答案


推荐阅读