PreferenceFragmentCompat has padding on PreferenceCategory that I can't get rid of

后端 未结 6 1428
小鲜肉
小鲜肉 2020-11-29 04:38

So I\'ve been trying to create a settings activity using androidx.preference.PreferenceFragmentCompat and it\'s all working fine.

However for some reason there is so

相关标签:
6条回答
  • 2020-11-29 04:54

    Actually there is a better hack to fix this, also with resource overriding:

    Create res/values-sw360dp-v13/values-preference.xml:

    <?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>
        <dimen name="preference_category_padding_start" tools:ignore="MissingDefaultResource,PrivateResource">0dp</dimen>
    </resources>
    

    The <bool> fixes the default value of iconSpacePreserved for all Preference; The <dimen> fixes the PreferenceCategory.

    EDIT: If you are using androidx.preference:preference:1.1.0-alpha01 or above, you won't need the preference_category_padding_start fix and config_materialPreferenceIconSpaceReserved only will be enough.

    0 讨论(0)
  • 2020-11-29 05:07

    If using AndroidX, you can add/remove extra padding by simply adding the following attribute to your Preferences and Preference Categories in XML:

    <androidx.preference.PreferenceScreen
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <Preference
            ...
            app:iconSpaceReserved="true/false"
            .../>
    
    </androidx.preference.PreferenceScreen>
    

    true adds extra padding, false removes it.

    0 讨论(0)
  • 2020-11-29 05:11

    This Bug has been Fixed in Latest Release of androidx preference

    Try Following Dependency in yout Build.Gradle

        implementation 'androidx.preference:preference:1.1.0'
    
    0 讨论(0)
  • 2020-11-29 05:12

    I solved by a workaround with styles. It works with PreferenceCategory.

    For Preference just use: app:iconSpaceReserved="false".

    [styles.xml]

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="preferenceTheme">@style/AppPreferenceTheme</item>
    </style>
    
    <style name="AppPreferenceTheme" parent="@style/PreferenceThemeOverlay.v14.Material">
        <item name="preferenceCategoryStyle">@style/FixForPreferenceCategoryStyle</item>
    
        <!-- <item name="android:textColor">#ffffffff</item> -->
        <!-- <item name="android:textColorSecondary">#b3ffffff</item> -->
        <!--when the check box is checked-->
        <!--<item name="colorControlNormal">#FF4081</item>-->
        <!--when the check box is unchecked -->
        <!--<item name="colorControlActivated">#81FF40</item>-->
    </style>
    
    <style name="FixForPreferenceCategoryStyle" parent="@style/Preference.Category.Material">
        <item name="android:layout">@layout/_preference_category_material</item>
    </style>
    
    <color name="_preference_fallback_accent_color">@color/colorAccent</color>
    

    [_preference_category_material.xml] copied from "@layout/preference_category_material".

    Replace @dimen/preference_category_padding_start with android:paddingLeft="0dp". Add custom color @color/_preference_fallback_accent_color.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        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="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft">
    
        <LinearLayout
            android:id="@+id/icon_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="start|center_vertical"
            android:orientation="horizontal">
            <android.support.v7.internal.widget.PreferenceImageView
                android:id="@android:id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:maxHeight="18dp"
                app:maxWidth="18dp"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="0dp"><!-- SET to 0dp -->
            <!--android:paddingLeft="@dimen/preference_category_padding_start"-->
    
            <TextView
                android:id="@android:id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:textAlignment="viewStart"
                android:textColor="@color/_preference_fallback_accent_color"/>
            <TextView
                android:id="@android:id/summary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="?android:attr/textColorSecondary"/>
        </LinearLayout>
    
    </FrameLayout>
    
    0 讨论(0)
  • 2020-11-29 05:14

    Rather than mucking with styles, I found it easier to just do it programmatically:

    private fun Preference.removeIconSpace() {
        setIconSpaceReserved(false)
        if (this is PreferenceGroup) {
            for (i in 0 until preferenceCount) {
                getPreference(i).removeIconSpace()
            }
        }
    }
    
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.settings, rootKey)
        …
        preferenceScreen.removeIconSpace()
    }
    
    0 讨论(0)
  • 2020-11-29 05:18

    The fix is currently released in alpha. You can use

    implementation 'androidx.preference:preference:1.1.0-alpha01'

    or the latest released one in your build.gradle

    0 讨论(0)
提交回复
热议问题