如何在PreferenceScreen中删除左缩进?

时间:2018-08-23 12:37:51

标签: android kotlin android-preferences

由于不赞成使用PreferenceFragment,所以我使用PreferenceFragmentCompat。

替换片段后,内容缩进了。:
After replacing the fragment, I get left indentation from the content.

缩进很可能是由于图标而出现的,但是我不使用它们(默认为无图标)。

我尝试将icon属性设置为android:icon="@null"android:color/transparent,但没有帮助。

替换片段的功能:

private fun replaceFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_layout, fragment)
            .commit()
}

content_main.xml

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我的设置片段:

class SettingsFragment : PreferenceFragmentCompat() {

companion object {

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private val sBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->

        val stringValue = value.toString()

        if (preference is ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            val index = preference.findIndexOfValue(stringValue)

            // Set the summary to reflect the new value.
            preference.setSummary(
                    if (index >= 0)
                        preference.entries[index]
                    else
                        null)

        } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.summary = stringValue
        }
        true
    }
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.preferences, rootKey)

    /**
     * Bind preference summary to value for lists and sorting list preferences
     */

    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_name)))
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_language)))

}

private fun bindPreferenceSummaryToValue(preference: Preference) {
    // Set the listener to watch for value changes.
    preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.context)
                    .getString(preference.key, ""))
}

preferences.xml

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_general">

    <android.support.v7.preference.EditTextPreference
        android:defaultValue="@string/nav_header_subtitle"
        android:key="@string/pref_key_name"
        android:summary="@string/nav_header_subtitle"
        android:title="@string/pref_title_your_name" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/pref_key_sound"
        android:summary="@string/pref_summary_sound"
        android:title="@string/pref_title_sound" />

    <android.support.v7.preference.ListPreference
        android:dialogTitle="Select language"
        android:entries="@array/settings_list_preference_languages_titles"
        android:entryValues="@array/settings_list_preference_languages_values"
        android:key="@string/pref_key_language"
        android:summary="Click to select language"
        android:title="Language" />

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_notifications">

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/notifications_new_message"
        android:title="@string/pref_title_new_notification_sound" />


    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="true"
        android:key="@string/pref_key_notifications_vibrate"
        android:summary="@string/pref_summary_vibrate"
        android:title="@string/pref_title_vibrate" />

</android.support.v7.preference.PreferenceCategory>

<!--Account Settings-->
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_account">

    <android.support.v7.preference.Preference
        android:key="@string/pref_key_logout"
        android:title="@string/pref_title_logout" />

    <android.support.v7.preference.Preference
        android:key="@string/pref_key_delete_account"
        android:title="@string/pref_title_delete_account" />
</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/pref_header_about">

    <android.support.v7.preference.Preference
        android:summary="@string/app_version"
        android:title="@string/pref_title_version" />

</android.support.v7.preference.PreferenceCategory>

styles.xml

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">?themePrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="themePrimary">@color/colorPrimary</item>

    <!-- PreferenceFragmentCompat -->
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

版本SDK

ext.support_version = '28.0.0-rc01'

2 个答案:

答案 0 :(得分:9)

如果您将代码迁移到androidx,则可以通过为每个首选项添加以下内容来轻松删除多余的填充:

app:iconSpaceReserved="false"

您还需要在xml文件的顶部添加以下名称空间:

xmlns:app="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

values-preference.xml文件放入res/values-sw360dp

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:tools="http://schemas.android.com/tools">
    <bool name="config_materialPreferenceIconSpaceReserved"
          tools:ignore="MissingDefaultResource,PrivateResource" >
        false
    </bool>
</resources>

通过深入研究库代码,您可以看到它在sw360dp-v13中具有一个资源文件,该资源文件将覆盖您的基本修改,例如app:iconSpaceReserved="false"