PreferenceFragmentCompat имеет дополнение к PreferenceCategory, от которого я не могу избавиться - программирование
Подтвердить что ты не робот

PreferenceFragmentCompat имеет дополнение к PreferenceCategory, от которого я не могу избавиться

Поэтому я пытался создать активность настроек с помощью androidx.preference.PreferenceFragmentCompat, и все это работает нормально.

Однако по некоторым причинам есть некоторые дополнения, присутствующие как в категориях предпочтений, так и в самих предпочтениях. Мне удалось избавиться от дополнения в настройках с помощью приложения: iconSpaceReserved = "false", но это не похоже на работу над категориями.

Образ

Я пробовал все различные темы: PreferenceThemeOverlay.v14.Material и т.д., Но они, похоже, не имеют значения

Вот мой код для всего!

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>

Мои зависимости...

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'

Спасибо за вашу помощь.

4b9b3361

Ответ 1

На самом деле есть лучший способ исправить это, также с переопределением ресурса:

Создать 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> фиксирует значение по умолчанию iconSpacePreserved для всех Preference; <dimen> исправляет PreferenceCategory.

ОБНОВЛЕНИЕ: Если вы используете androidx.preference:preference:1.1.0-alpha01 или выше, вам не понадобится исправление preference_category_padding_start и достаточно только config_materialPreferenceIconSpaceReserved.

Ответ 2

Я решил обходным путем со стилями. Он работает с PreferenceCategory.

Для предпочтения просто используйте: 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] скопирован из "@layout/preference_category_material".

Замените @dimen/preference_category_padding_start с помощью android: paddingLeft = "0dp". Добавьте собственный цвет @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>

Ответ 3

Исправление в настоящее время выпущено в альфа-версии. Ты можешь использовать

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

или последний выпущенный в вашем build.gradle

Ответ 4

При использовании AndroidX вы можете добавить/удалить дополнительные отступы, просто добавив следующий атрибут в ваши предпочтения и категории предпочтений в XML:

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

    <Preference
        ...
        app:iconSpaceReserved="true/false"
        .../>

</androidx.preference.PreferenceScreen>

Значение true добавляет дополнительный отступ, значение false удаляет его.