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

问题描述 投票:13回答:4

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

Android: How to maximize PreferenceFragment width (or get rid of margin)?

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

有人有什么想法吗?

android margin preference
4个回答
10
投票

您可以自定义您的复选框,如下所示:My Preference.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”。


10
投票

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

android:icon="@null"

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

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

0
投票

user1482130描述的custom_checkbox_preference_layout.xml也可以不对其他类型的首选项进行修改,例如listpreferences!非常有用

android:layout="@layout/custom_checkbox_preference_layout">

0
投票

如果要创建首选项,请将ContextThemeWrapper作为参数传递给新的首选项

TypedValue themeTypedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeTypedValue.resourceId);
Preference preference = new Preference(contextThemeWrapper);

我在这篇文章中找到了它:Dynamically creating Preference Screens on Android

© www.soinside.com 2019 - 2024. All rights reserved.