PreferenceFragmentCompat在PreferenceCategory上有填充,我无法摆脱 [英] PreferenceFragmentCompat has padding on PreferenceCategory that I can't get rid of

查看:118
本文介绍了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>

我的依赖对象是...

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'

感谢您的帮助.

推荐答案

实际上,有一个更好的技巧可以解决此问题,同时还可以覆盖资源:

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

创建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>

<bool>修复了所有Preference的默认值iconSpacePreserved<dimen>修复了PreferenceCategory.

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

如果使用的是androidx.preference:preference:1.1.0-alpha01或更高版本,则不需要preference_category_padding_start修复程序,仅config_materialPreferenceIconSpaceReserved就足够了.

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆