Android:如何在偏好中调整边距/填充?

jcl*_*ova 14 android margin preference

在我要求删除PreferenceActivity/PreferenceFragment的填充之前:

Android:如何最大化PreferenceFragment宽度(或摆脱保证金)?

这次我无法在标题文本之前调整边距/填充.(见下图).

在此输入图像描述

有人有任何想法吗?

小智 12

您可以自定义您的复选框,如下所示:MyPreference.xml

<CheckBoxPreference android:key="testcheckbox" 
    android:title="Checkbox Title" 
    android:summary="Checkbox Summary..." 
    android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>
Run Code Online (Sandbox Code Playgroud)

和custom_checkbox_preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="16dip"
    android:paddingRight="?android:attr/scrollbarSize">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="0dp"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

重点是android:minWidth ="0dp".


Mic*_*kis 11

对于任何面临此问题并且不想通过自定义布局的人.我想注意添加:

android:icon="@null"
Run Code Online (Sandbox Code Playgroud)

解决了左侧空白区域的问题.

<PreferenceScreen android:icon="@null" android:title="Your title" android:summary="Your summary" />
Run Code Online (Sandbox Code Playgroud)

  • 如果使用 `androidx.preference:preference:1.0.0` 或更高版本,它不起作用。 (3认同)
  • 对每个首选项使用`app:iconSpaceReserved="false"` (2认同)

Ish*_*mar 5

截至 2020 年 2 月 16 日,此空间用于图标。要摆脱这个空间,请使用:

爪哇

    preference.setIconSpaceReserved(false);
Run Code Online (Sandbox Code Playgroud)

XML

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

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

</androidx.preference.PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

True 添加填充。False 将其删除。

PS:使用的依赖项

implementation 'androidx.preference:preference:1.1.0'
Run Code Online (Sandbox Code Playgroud)