方向更改时复选框文本不会更改

发布于 2024-12-25 17:01:38 字数 2202 浏览 1 评论 0原文

复选框文本在方向更改时不会更改

我的屏幕有不同的布局文件(一个在布局中,一个在布局区域中),唯一的区别是文件中的某些 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 Text Does Not Change on Orientation Change

I have differing layout files (one in layout and one in layout-land) for a screen with the only difference being that some UI widgets in the files point to different string (longer string for landscape mode). For TextView widgets the correct resource Strings are displayed, but for CheckBoxes the text displayed stays the same during all orientation changes.

./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>

So with the above, on an orientation change label1's text changes between label1Short and label1Long. But checkBox1's text stays the same as the text that was correct for the device orientation when the screen was first viewed.

Any thoughts? Thanks
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2025-01-01 17:01:38

即使配置更改(在您的情况下方向更改),复选框小部件也会保存其状态。作为问题的解决方案,您可以自己定义 Activity 的 onConfigurationChange 方法。

CheckBox widget save it state even after configuration change (orientation change in your case). As solution of your problem you can define Activity's onConfigurationChange method by yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文