android - 横向 - 如何保留数据和布局属性?
问题描述
你好 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
我想澄清一下:
- 如果我使用,为什么布局没有被读取
android:configChanges
? - 有没有办法可以使方向不删除在保持布局属性的同时创建的数据?
我的横向视图的完整 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 相关数据。
了解更多信息 Android ViewModel
推荐阅读
- java - Android:FusedLocationProvider,每隔几秒获取一次位置
- python - 如何从C访问python字典?
- javascript - 基于节点红色温度的灯或风扇
- javascript - 如何在 (window).resize 之后完全删除 jquery 函数
- vb.net - 循环中的sqlite更新需要很长时间
- elasticsearch - ElasticSearch - 列表元素中的模糊搜索
- dji-sdk - 如何使用 DJI mobile sdk 连接 DJI 产品
- express - 无法在中间件中获取标头令牌
- sql - 求和时选择值
- c# - 每次更新时都会刷新带有图像的表