android - 复选框文本不会随方向更改而更改

标签 android android-widget

复选框文本不会随方向改变而改变

对于一个屏幕,我有不同的布局文件(一个在布局中,一个在布局中),唯一的区别是文件中的一些 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/

相关文章:

java - 多个 TextView 具有一个 ID

Android:AlertDialog 中的按钮

android - 在android中自动生成dimen.xml

java - 如何设置图像按钮的文本(Android)?

android - ./gradlew 没有这样的文件或目录 - Android studio 2.3

android - 为什么运行时无法识别 "res/values/strings.xml"中定义的字符串

android - 如何自定义布局(高度和宽度)以及布局的容器?

android - 处理 EditText 中可绘制对象的点击事件

android - 通过选择* Test.java文件构建Gradle

Android Twitter 集成不适用于不同的操作系统版本