2011-03-17 231 views
17
<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> 

我需要改变PrefereceCategoryCheckboxPreferenceandroid:summary大小的android:title字体大小。这些标签没有任何android:textSize属性。任何人都可以帮助我如何得到这个?如何更改字体大小PreferenceScreen

回答

49

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

这对我很有用。但是,如何获得系统字体和边距以及偏好列表项的颜色并将它们添加到mylayout.xml? – degratnik 2012-02-07 11:01:08

+5

非常感谢您的提示。我有一条评论可以改善你的回答。您应该将textSizes指定为“sp”或“dp”,但不能将其定义为“px”。使用“dp”将使字体在所有设备上具有相同的物理尺寸,“sp”大致类似于“dp”,但尺寸将遵循全局字体大小的设备设置。Android 4上的Settings-Screen中的大/中/小尺寸)。我的意见是,“sp”在这里是最合适的。 – 2012-09-15 11:51:36

+0

如何更改复选框首选项的默认蓝色drawable。 – Dory 2013-09-17 06:36:04

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

对于我来说,发表praneetloke布局做出的部件从具有相关小部件的偏好消失,例如一个复选框。有一个带有id/widget_frame的LinearLayout,这是widget所需要的。以下工作适用于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的非全息版本。

+0

这是最好的答案。惊人! – 2013-09-17 03:23:46

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); 
    } 
1

实际上,你可以看看这是在布局文件夹中的特定平台设在使用的布局:

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

首布局与preference_前缀开头。

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>