android - 自定义首选项标题字体大小大于 android 5 中的标准首选项

标签 android android-preferences android-5.0-lollipop

我的应用程序具有自定义布局的自定义首选项,在 android 5 上试用我的应用程序后,我注意到我的自定义首选项看起来与其他首选项不同(字体大小、颜色、填充)。所以我认为这将是一个简单的修复,只需从 android 5 SDK 中获取 preference.xml,合并我的更改并将新布局放置到 layout-v21 文件夹中。它修复了填充和颜色问题,但不是更大的标题大小。

我的自定义首选项构造函数如下所示:

public SeekBarPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.preference_seekbar);
}

preference_seekbar.xml(刚刚将Seekbar 添加到原始布局):

<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: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"
        />

    <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:singleLine="true"
            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" />

        <SeekBar android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+android:id/title"
            android:layout_toEndOf="@android:id/summary"
            android:layout_toStartOf="@android:id/widget_frame"/>

    </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_vertical"
        android:orientation="vertical" />

</LinearLayout>

你可以注意到上面的标题小部件有这个原始属性:android:textAppearance="?android:attr/textAppearanceLarge" 这应该使字体变大,但是有一个问题为什么标准偏好标题如果使用相同的布局(只是没有搜索栏),字体会变小吗?

最佳答案

有同样的问题,找到了solution here .

虽然我使用 textAppearanceListItem 作为标题 TextView 因为它似乎更适合我。

<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:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd">

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dip"
        android:layout_marginEnd="8dip"
        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_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

        <!-- 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:layout_below="@android:id/summary"
            android:layout_alignStart="@android:id/title"
            android:minWidth="@dimen/preference_widget_width"
            android:gravity="center"
            android:orientation="vertical" />

        <SeekBar android:id="@+android:id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/summary"
            android:layout_toEndOf="@android:id/widget_frame"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</LinearLayout>

关于android - 自定义首选项标题字体大小大于 android 5 中的标准首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077306/

相关文章:

android - 如何将空白图标空间添加到 preferencefragment

android - 将 Android camera2 api YUV_420_888 转换为 RGB

android - FFMPEG 视频裁剪太慢

android - Android 中的 IDN 支持

android - Gradle Build 卡在生成调试源上

Android L FAB 按钮阴影

android - Pre-Lollipop 设备上 ImageButton 的提升效果

android - Android phonegap GPS 精度

android - 如何在首选项摘要中显示 Android 首选项的当前值?

android - 如何为从 2.X 到 4.X 的所有 Android 版本使用相同的首选项屏幕集?