【问题标题】:How to remove left indent in the PreferenceScreen?如何删除 PreferenceScreen 中的左缩进?
【发布时间】:2018-08-23 12:37:50
【问题描述】:

由于不推荐使用 PreferenceFragment,我使用 PreferenceFragmentCompat。

替换片段后,我从内容中得到了左缩进。:

最有可能因为图标而出现缩进,但我不使用它们(默认为无图标)。

我尝试将图标属性设置为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'

【问题讨论】:

标签: android kotlin android-preferences


【解决方案1】:

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

app:iconSpaceReserved="false"

您还需要将以下命名空间添加到 xml 文件的顶部:

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

【讨论】:

  • 那么 PreferenceCategory 呢
  • 此解决方案适用于“com.android.support:preference-v7:28.0.0”
  • 正是我想要的。 +1
  • 我们可以应用到所有的 PreferenceCategory 吗?
  • 属性文档如何发誓默认情况下它是“假的”,这很有趣。是的,您也可以将其应用于 PreferenceCategory。
【解决方案2】:

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>

通过挖掘库代码,您可以看到它在values-sw360dp-v13 中有一个资源文件,它覆盖了您的基本修改,例如app:iconSpaceReserved="false"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2012-12-24
    • 1970-01-01
    • 2019-09-03
    • 2014-08-08
    相关资源
    最近更新 更多