Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

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

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 some padding present on both the preference categories and the preferences themselves. I managed to get rid of the padding on the preferences by using app:iconSpaceReserved="false" but this doesn't seem to work on the categories.

Image

I've tried all the various themes, PreferenceThemeOverlay.v14.Material etc but they don't seem to make a difference

Here is my code for everything!

SettingsActivity.java

import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        Toolbar toolbar = findViewById(R.id.settings_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }

    }
}

activity_settings.xml

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".SettingsActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/settings_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            style="@style/ToolbarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_settings"
            android:textColor="@color/font_dark_primary" />

    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:name="com.henrytwist8gmail.fullcart.SettingsTestFragment"
        android:tag="settings_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/settings_toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

SettingsTestFragment.java

import android.os.Bundle;

import androidx.preference.PreferenceFragmentCompat;

public class SettingsTestFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        setPreferencesFromResource(R.xml.preferences_test, rootKey);
    }
}

preferences_test.xml

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

    <PreferenceCategory android:title="test" >

        <Preference android:title="testPref" />
    </PreferenceCategory>

</PreferenceScreen>

My dependencies are...

implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.preference:preference:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...