Android PreferenceFragmentCompat空白空间

时间:2018-08-28 16:52:33

标签: android fragment android-support-library preferences

我正在使用PreferenceFragmentCompat(来自'com.android.support:preference-v7:28.0.0-rc02')来创建设置菜单。

问题是我在每个项目的左侧留了一个黑色的空格:black space

我通过为每个选项添加图标来“解决”该问题,但我希望 PreferenceCategory的标题左对齐

有人知道该怎么做吗?

您可以在这里查看我的布局:https://github.com/systemallica/ValenBisi/blob/master/app/src/main/res/xml/fragment_settings.xml

我尝试使用android:iconSpaceReserved="false",但没有区别。

1 个答案:

答案 0 :(得分:1)

我的解决方案是这样的:

custom_preference_category_material.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="start"
            android:paddingRight="?android:attr/listPreferredItemPaddingRight"
            android:textAlignment="viewStart"
            android:textColor="?colorAccent" />

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

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="preferenceTheme">@style/CustomPreferenceTheme</item>
</style>

<style name="CustomPreferenceTheme" parent="@style/PreferenceThemeOverlay">
    <item name="preferenceCategoryStyle">@style/CustomPreferenceCategory</item>
    <item name="preferenceStyle">@style/CustomPreference</item>
    <item name="checkBoxPreferenceStyle">@style/CustomCheckBoxPreference</item>
    <item name="dialogPreferenceStyle">@style/CustomDialogPreference</item>
    <item name="switchPreferenceCompatStyle">@style/CustomSwitchPreferenceCompat</item>  <!-- for pre lollipop(v21) -->
    <item name="switchPreferenceStyle">@style/CustomSwitchPreference</item>
    <item name="seekBarPreferenceStyle">@style/CustomSeekBarPreference</item>
    <item name="preferenceScreenStyle">@style/CustomPreferenceScreen</item>
</style>

<style name="CustomPreferenceCategory" parent="Preference.Category.Material">
    <item name="android:layout">@layout/custom_preference_category_material</item>
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomPreference" parent="Preference.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomCheckBoxPreference" parent="Preference.CheckBoxPreference.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomDialogPreference" parent="Preference.DialogPreference.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomSwitchPreferenceCompat" parent="Preference.SwitchPreferenceCompat.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomSwitchPreference" parent="Preference.SwitchPreference.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomSeekBarPreference" parent="Preference.SeekBarPreference.Material">
    <item name="iconSpaceReserved">false</item>
</style>

<style name="CustomPreferenceScreen" parent="Preference.PreferenceScreen.Material">
    <item name="iconSpaceReserved">false</item>
</style>