Android:如何调整偏好设置中的边距/填充?

14

嗨@jclova,我遇到了同样的问题,我不想通过自定义布局来检查复选框。你是如何解决这个问题的? - Dory
我怀疑你所看到的左侧填充是一个"空的" android:icon。你可以尝试提供自己的drawable,并设置尽可能小的尺寸,看看是否有所帮助。 - Stan
类似的问题在这里得到了解答:https://dev59.com/bWMl5IYBdhLWcg3wTljY - Vivek
5个回答

13

您可以像这样自定义复选框: MyPreference.xml

<CheckBoxPreference android:key="testcheckbox" 
    android:title="Checkbox Title" 
    android:summary="Checkbox Summary..." 
    android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>

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

关键在于android:minWidth="0dp"。


你好,我正在使用自定义布局来设置CheckBoxPreference。但我的问题是具有ID "@+android:id/widget_frame" 的线性布局在其左侧和右侧具有一些填充。如何去除它。先谢谢了。 - Hradesh Kumar

11

对于任何遇到这个问题但不想经过自定义布局的人。我想指出的是添加:

android:icon="@null"

通过解决左侧空白的问题,我解决了我的问题。

<PreferenceScreen android:icon="@null" android:title="Your title" android:summary="Your summary" />

完美的解决方案!应该排在第一位。 - romaneso
3
如果使用 androidx.preference:preference:1.0.0 或更高版本,则无法正常工作。 - Joonsoo
2
对于每个偏好设置,请使用app:iconSpaceReserved="false" - coder

5
截止2020年2月16日, 这个空间是为图标而设计的。要去掉这个空间,请使用: JAVA
    preference.setIconSpaceReserved(false);

XML

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

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

</androidx.preference.PreferenceScreen>

True表示添加填充,False表示删除填充。

PS: 使用了依赖项。

implementation 'androidx.preference:preference:1.1.0'

也使用了这个依赖库:com.android.support:preference-v7:28.0.0 - Rodgers Andati
支持库中的Preference现已弃用,请使用androidx。 - Ishaan Kumar
1
@VictorOkech ;) - Ishaan Kumar

0

由user1482130描述的custom_checkbox_preference_layout.xml也可用于其他类型的首选项,例如listpreferences!非常有用

android:layout="@layout/custom_checkbox_preference_layout">

0
如果您正在动态创建首选项,请将ContextThemeWrapper作为参数传递给新的Preference。
TypedValue themeTypedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeTypedValue.resourceId);
Preference preference = new Preference(contextThemeWrapper);

我在这篇文章中找到了它:动态创建Android上的首选项屏幕


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接