如何在PreferenceScreen中更改字体大小

时间:2011-03-17 04:21:57

标签: android android-layout android-preferences

<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>

我需要更改android:title字体大小PrefereceCategoryCheckboxPreferenceandroid:summary大小。这些代码没有任何android:textSize属性。任何人都可以帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:56)

最后,我能够通过传递一个布局来解决我的问题。也许这对某人有帮助。 这是我修改过的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>

<强> 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>

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

答案 1 :(得分: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>

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

答案 2 :(得分:4)

我修改了Kumar对ICS及以上的答案。我将此布局放在values-v11中,以设备API级别11 +。

<?xml version="1.0" encoding="utf-8"?>
<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="18dp"
        android:textColor="?android:attr/textColorPrimary"
        android:paddingLeft="8dip"/>

    <TextView android:id="@+android:id/summary"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="14dp"
        android:textColor="?android:attr/textColorSecondary"
        android:paddingLeft="8dip"/>

</LinearLayout>

答案 3 :(得分:2)

您实际上可以查看位于以下位置的布局文件夹中特定平台中使用的布局:

<android-sdk>\platforms\android-<level>\data\res\layout

首选项布局以首选项_ 前缀开头。

答案 4 :(得分:0)

看看我对此的回答: Android PreferenceScreen title in two lines

如果需要,您也可以更改字体本身。 只需添加:

Typeface myTypeface = Typeface.createFromAsset(textView.getContext().getAssets(),"fonts/your_font_name.ttf");
    if (textView != null) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getContext().getResources().getDimension(R.dimen.text_size_from_dimen));
        textView.setTypeface(myTypeface);
    }

答案 5 :(得分:0)

只是对布局代码进行了一次小修改。 “@ + android:id / summary”导致我无法解析符号。所以我删除'+'符号,代码工作

<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="40px"/>  

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

</LinearLayout>
相关问题