横向-如何保留数据和布局属性



Hello Stack Overflow社区,

我一直试图让我的设备在每次将方向从纵向更改为横向时都不会擦除数据。

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

<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代码。

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

了解更多信息安卓ViewModel

也可以参考这个答案

经过进一步测试,问题似乎不在android:configChanges中,而是RelativeLayout的打开标签的位置导致了所有这些问题。我只需要把它改成下面的就行了:

我的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 ...>
<LinearLayout...>
<LinearLayout
...
</LinearLayout>
<View ... />
<LinearLayout
...
</LinearLayout>

</LinearLayout>

<RelativeLayout...>
<Button .../>
</RelativeLayout>
</LinearLayout>
</ScrollView>

下面是完成所有代码更改的详细解决方案,包括我的XML文件:

更新AndroidManifest.xml文件中的代码,在活动标签中包括android:configChanges的附加代码,如下所示:

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

然后,我不得不转到我的activity_main.xml并更改RelativeLayout打开标记的位置,因为当调用从纵向到横向的方向时,这是一些按钮被隐藏的原因。

旧代码格式:

<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 ...>
<RelativeLayout...> <-- remove it from here
<LinearLayout...>
<LinearLayout
...
</LinearLayout>
<View ... />
<LinearLayout
...
</LinearLayout>

</LinearLayout>

<Button .../>
</RelativeLayout>
</LinearLayout>
</ScrollView>

我的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 ...>
<LinearLayout...>
<LinearLayout
...
</LinearLayout>
<View ... />
<LinearLayout
...
</LinearLayout>

</LinearLayout>

<RelativeLayout...> <-- add it here
<Button .../>
</RelativeLayout>
</LinearLayout>
</ScrollView>

完整的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="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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="16dp"
android:layout_marginRight="16dp"
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="16dp"
android:layout_marginRight="16dp"
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="16dp"
android:layout_marginRight="16dp"
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="16dp"
android:layout_marginRight="16dp"
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="16dp"
android:layout_marginRight="16dp"
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="16dp"
android:layout_marginRight="16dp"
android:onClick="addOnePenaltyTeamB"
android:text="@string/bt_penalty_plus_1" />
</LinearLayout>

</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>

最新更新