【问题标题】:How can I change font size in PreferenceScreen如何更改 PreferenceScreen 中的字体大小
【发布时间】:2011-03-17 04:21:56
【问题描述】:
<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:summaryandroid:title 字体大小。这些标签没有任何android:textSize 属性。谁能帮我怎么弄到这个?

【问题讨论】:

    标签: android android-layout android-preferences


    【解决方案1】:

    最后,我只需通过一个布局就可以解决我的问题。也许这会对某人有所帮助。 这是我修改后的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>
    

    如有任何疑问,请询问我....保持微笑。如果这有用,请给我一条评论。

    【讨论】:

    • 这对我很有用。但是如何获取首选项列表项的系统字体、边距和颜色并将它们添加到 mylayout.xml?
    • 非常感谢您的提示。我有一条评论可以改善您上面的答案。您应该将 textSizes 指定为“sp”或“dp”,而不是“px”。使用“dp”将使字体在所有设备上具有相同的物理大小,“sp”大致类似于“dp”,但大小将遵循全局字体大小的设备设置(例如设置中的大/中/小尺寸- Android 4 上的屏幕)。我的意见是“sp”在这里最合适。
    • 如何更改复选框首选项的默认蓝色可绘制对象。
    • 当我使用上面的 xml 作为偏好时,在下面的 id @+android:id/summary @+android:id/title 我在 xml 中出现错误?你能帮我解决这个问题吗?
    • 不应该删除ID之前的+吗?我们不是在这里创建新的 id,只是使用 android 命名空间中的现有 ID,是吗?
    【解决方案2】:

    对我来说,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 的非holo 版本。

    【讨论】:

    • 这是最好的答案。太棒了!
    【解决方案3】:

    我针对 ICS 及更高版本修改了 Kumar 对此的回答。我将此布局放置在 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>
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        看看我对此的回答: 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);
            }
        

        【讨论】:

          【解决方案6】:

          只是对布局代码的小修改。 “@+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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-07-17
            • 2016-09-01
            • 2011-11-03
            • 2012-04-17
            • 2010-12-13
            • 2013-08-03
            • 2013-09-28
            • 2012-07-22
            相关资源
            最近更新 更多