如何在PreferenceScreen中更改字体大小

Ani*_*mar 19 android android-preferences android-layout

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category"
        android:textSize="20px">

        <CheckBoxPreference
            android:title="Checkbox Preference"
            android:defaultValue="false"
            android:summary="This preference can be true or false"
            android:key="checkboxPref" />

    </PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

我需要改变android:title的字体大小PrefereceCategory,CheckboxPreferenceandroid:summary大小.这些标签没有任何android:textSize属性.任何人都可以帮我解决这个问题吗?

Ani*_*mar 58

最后,我能够通过传递一个布局来解决我的问题.也许这对某人有帮助.这是我修改过的preference.xml,见下面的代码.我们可以将自己的属性应用于首选标题和摘要.

preference.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="First Category"
        android:textSize="20px"
        android:layout="@layout/mylayout">             

        <CheckBoxPreference
            android:title="Checkbox Preference"
            android:defaultValue="false"
            android:summary="This preference can be true or false"
            android:key="checkboxPref"
            android:layout="@layout/mylayout"/>

    </PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

mylayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@android:id/title"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="40sp"/>  

    <TextView android:id="@android:id/summary"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="26sp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

请问我是否有任何疑问....继续微笑.如果这很有用,请给我一个评论.

  • 非常感谢您的提示.我有一条评论可以改善你上面的答案.您应将textSizes指定为"sp"或"dp",并将其指定为"px".使用"dp"将使字体在所有设备上具有相同的物理尺寸,"sp"大致类似于"dp",但尺寸将遵循全局字体大小的设备设置(例如,设置中的大/中/小尺寸) Android屏幕4).我的观点是"sp"在这里是最合适的. (5认同)
  • 这对我很有用。但是如何获取系统字体、边距和首选项列表项的颜色并将它们添加到 mylayout.xml 中? (2认同)

Yoj*_*mbo 11

对我来说,praneetloke发布的布局使得小部件从具有关联小部件的首选项中消失,例如复选框.小部件需要具有id/widget_frame的LinearLayout.以下适用于我,在Android 2.3到4.2.2上.我需要做的唯一更改是在包含首选项图标的布局上设置android:visibility ="gone",这在Gingerbread中不可用.

<?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:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="8dip"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minWidth="0dp"
        android:orientation="horizontal"
        android:visibility="gone" >

        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="48dp"
            android:paddingRight="8dip" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="6dip"
        android:paddingRight="8dip"
        android:paddingTop="6dip" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />
    </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:minWidth="48dp"
        android:orientation="vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

参考:布局中使用的默认维度是GrepCode /res/values/dimens.xml.实际的首选项布局在这里是GrepCode /res/layout/preference_holo.xml.请注意,还有一个名为preference.xml的非全息版本.