为自定义首选项自动设置标题和摘要

Setting title and summary automatically for custom preference

我在设置 activity 中使用自定义布局制作了自定义对话框首选项。这里的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground"
    >
    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="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:maxLines="1"
            android:textAppearance="?android:attr/textAppearanceLarge"
            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_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4"
            />
    </RelativeLayout>

    <!-- ... -->
</LinearLayout>

除了标题和摘要之外的所有内容都显示了,即使它们有 @android:id 也没有显示任何值。这似乎对某些人有用,但对我不起作用。

我从这里获取代码:layout, class and xml preference。如果您仔细观察,link 标题和 xml 首选项文件值的摘要视图没有任何变化,但它们确实如此。如何? 这里我说的是 android:titleandroid:summary 值。

我不会添加我的自定义首选项代码,但问题应该不存在。我有 4 个构造函数,我正确使用 setLayoutResource

通常在首选项中,您应该在摘要中添加“%s”

android:summary="%s"

问题是 LinearLayout 没有方向。如果我添加 android:orientation="vertical" 一切都会正确显示。