PreferenceCompatFragment显示EditText对话框后布局错乱求助
偏好设置Fragment弹出EditText对话框后布局错乱问题求助
基于PreferenceFragmentCompat构建的偏好设置Fragment,每当弹出EditText对话框,偏好列表的布局就会随机出现错乱。旋转屏幕(使用该Activity的默认配置变更行为)或切换到其他Fragment再切回,布局即可恢复正常。已在onResume/onPause中管理过变更监听器,移除后问题无变化,确定此问题与监听器无关。
尝试过takisoft修复库,并未解决该问题。试过各种搜索词都没找到解决方案,求解决思路。
最小复现示例(MRE)
Fragment代码
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); } }
Activity代码
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(); } }
Activity布局文件
<?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
<?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 Takisoft库 - 1.1.0
复现过程中发现一个现象:最初使用appcompat 1.6.1、material 1.10.0时,存在操作栏遮挡偏好设置分类名称的问题;将任意一个库更新到当前版本后,操作栏问题解决,但出现了偏好布局错乱的bug。
内容的提问来源于stack exchange,提问作者Brian S
相关产品推荐
相关产品推荐

