PreferenceCompatFragment - 显示文本编辑对话框时布局混乱

移动开发 2026-07-09

在用PreferenceFragmentCompat构建的偏好设置片段进行测试时,我发现偏好列表的外观在显示编辑文本对话框时似乎会随机混乱。若我旋转屏幕(使用这个Activity的默认配置变更行为)或从另一个片段切换再回到这个片段,正确的外观就会恢复。我在onResume/onPause里管理一个变更监听器,但移除它也没有改变,所以我知道这与之无关。

我也尝试了takisoft修复库,假设它仍然相关。虽然它可能对其他问题有效,但对这个问题无效。

有什么想法吗?我已经用尽各种搜索词寻找修复方法,但没有发现任何结果。


编辑:一个最小可重现的示例,马上给出……

import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;

public final class PreferencesFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle icicle, String rootKey) {
        setPreferencesFromResource(R.xml.prefs, rootKey);
    }
}
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public final class Prefs extends AppCompatActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        EdgeToEdge.enable(this);
        setContentView(R.layout.app_prefs);

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, new PreferencesFragment())
                .commit();
    }

}
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container"
    />
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.preference.PreferenceCategory
        android:title="Category">

        <androidx.preference.EditTextPreference
            android:key="pref1"
            app:useSimpleSummaryProvider="true"
            android:title="Preference 1" />

        <androidx.preference.EditTextPreference
            android:key="pref2"
            app:useSimpleSummaryProvider="true"
            android:title="Preference 2" />

        <androidx.preference.EditTextPreference
            android:key="pref3"
            app:useSimpleSummaryProvider="true"
            android:title="Preference 3" />

    </androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.PrefTest" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="android:fitsSystemWindows">true</item>
    </style>
</resources>

关于库和版本:

androidx.appcompat - 1.7.1
com.google.android.material - 1.13.0
androidx.preference - 1.2.1

(本来并不打算把那份列表放在代码块中,但编辑器就是不接受其他格式。)

我在尝试复现这个问题时确实注意到一件有趣的事:起初我在处理时,操作栏遮挡了偏好类别名称。此时appcompat的版本是1.6.1,material的版本是1.10.0。当我把它们中的任意一个更新到Android Studio提示的当前版本(见上文),操作栏的问题消失了,但偏好布局的错误却开始出现。

当我尝试Takisoft库时,是1.1.0。

解决方案

来自问题中的评论:

显然原因是 android:fitsSystemWindows 的声明放错了位置。它应该是布局根元素的一个属性,而不是主题的一部分。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章