复选框文本不会随方向改变而改变
对于一个屏幕,我有不同的布局文件(一个在布局中,一个在布局中),唯一的区别是文件中的一些 UI 小部件指向不同的字符串(横向模式的字符串较长)。对于 TextView 小部件,会显示正确的资源字符串,但对于复选框,显示的文本在所有方向更改期间保持不变。
./layout/tabA.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label1"
style="@style/TextBoxLabel.Header.WithInputsOnScreen"
android:text="@string/label1Short"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_below="@id/label1"
android:text="@string/checkBox1Short"
/>
</RelativeLayout>
</ScrollView>
./layout-land/tabA.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label1"
style="@style/TextBoxLabel.Header.WithInputsOnScreen"
android:text="@string/label1Long"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_below="@id/label1"
android:text="@string/checkBox1Long"
/>
</RelativeLayout>
</ScrollView>
因此,在方向改变时,label1 的文本在 label1Short 和 label1Long 之间改变。但是 checkBox1 的文本与首次查看屏幕时设备方向的正确文本保持一致。
有什么想法吗?谢谢 迈克
最佳答案
CheckBox 小部件即使在配置更改(在您的情况下方向更改)之后也会保存它的状态。作为您问题的解决方案,您可以自己定义 Activity 的 onConfigurationChange 方法。
关于android - 复选框文本不会随方向更改而更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779267/