PreferenceFragmentCompat

PreferenceFragmentCompat

本文介绍了PreferenceFragmentCompat 在 PreferenceCategory 上有填充,我无法摆脱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试使用 androidx.preference.PreferenceFragmentCompat 创建一个设置活动,并且一切正常.

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

但是由于某种原因,首选项类别和首选项本身都存在一些填充.我设法通过使用 app:iconSpaceReserved="false" 摆脱了首选项的填充.但这似乎不适用于类别.

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.

图片

我尝试了所有各种主题,PreferenceThemeOverlay.v14.Material 等,但它们似乎没有什么不同

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

这是我的所有代码!

SettingsActivity.java

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

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

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

preferences_test.xml

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

    <PreferenceCategory android:title="test" >

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

</PreferenceScreen>

我的依赖项是...

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'

推荐答案

最新版本的androidx首选项中已修复此Bug

This Bug has been Fixed in Latest Release of androidx preference

在你的Build.Gradle

    implementation 'androidx.preference:preference:1.1.0'

这篇关于PreferenceFragmentCompat 在 PreferenceCategory 上有填充,我无法摆脱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 03:56