【发布时间】:2019-03-08 08:40:25
【问题描述】:
在我的 Android 应用中,我定义了一个非常基本的 PreferenceScreen:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:entries="@array/test_entries"
android:entryValues="@array/test_values"
android:key="key_test1"
android:title="Test title 1" />
<CheckBoxPreference
android:key="key_test2"
android:summary="Test description 1"
android:title="Test title 2" />
</PreferenceScreen>
并实现了对应的PreferenceFragmentCompat类:
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
}
}
我正在使用的依赖项:
implementation "com.android.support:preference-v7:28.0.0"
以及托管Fragment 的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.activity.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize" />
<fragment
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
一切正常,除了每个首选项的左侧有一个巨大的空白区域:
这个问题的原因是什么,我该如何预防?
编辑:
问题似乎是为图标保留了空间,即使我们没有为首选项条目使用图标。
将首选项的icon 属性设置为@null 无效。
将iconSpaceReserved 属性设置为false 可以解决Preference 项目的问题,但对PreferenceCategory 项目没有影响。
【问题讨论】:
-
您是如何添加
Toolbar的?你在你的布局中添加了吗?你会粘贴布局吗?也许添加了起始边距? -
即使删除列表首选项也会发生这种情况吗?尝试从非常简单的东西开始,例如复选框、一个单词作为标题、一个单词作为描述。
-
@Mohsen 我已经添加了布局代码
-
@justanoob 删除这个:
androidx.navigation.fragment.NavHostFragment看看它是否有帮助。此外,您还没有迁移到 AndroidX,但这可能会导致问题。
标签: android sharedpreferences preferencefragment