【问题标题】:Setting title and summary automatically for custom preference为自定义首选项自动设置标题和摘要
【发布时间】:2017-04-13 00:22:39
【问题描述】:

我在设置活动中使用自定义布局创建了自定义对话框首选项。这里的布局:

<?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。这似乎适用于某些人,但不适用于我。

我从这里获取了我的代码:layoutclassxml preference。如果您仔细观察,没有任何操作将标题和摘要视图链接到 xml 首选项文件值,但它们确实如此。如何? 这里我说的是android:titleandroid:summary 值。

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

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

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

    android:summary="%s"

    【讨论】:

    • 我不清楚,我说的是 android:titleandroid:summary 值未应用于我的布局中的文本视图。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2017-01-06
      • 2015-07-13
      • 1970-01-01
      • 2011-10-28
      • 2016-05-16
      相关资源
      最近更新 更多