Android PreferenceFragmentCompat 空格

标签 android fragment android-support-library preferences

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

问题是我在每个项目的左边都有一个黑色空间:black space

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

有人知道怎么做吗?

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

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

最佳答案

我的解决方案是这样的:

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>

样式.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>

关于Android PreferenceFragmentCompat 空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062902/

相关文章:

android - 在所有库中导入应用程序兼容性依赖项

android - 使用不同版本的 Android 支持库的解决方法

android - 从android上传文件到wcf

Android.os.Build 数据示例,请

android - 对于 BLE,从字符串转换为十六进制值意味着什么?

android - 如何在 Fragment 中获取 onWindowFocusChanged?

android - ViewPager FragmentPagerAdapter Nullpointer

java - 使用 onItemClickListener 单击时,从数据库获取的 gridview 中的图像未全屏显示

android - 在选项卡布局 Activity 中,我的 fragment 在滑动时不会刷新

android - 无法在 android studio 3.4.1 中解析符号 'NotificationCompat '