[英]How can I change font size in PreferenceScreen
<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
字體大小PrefereceCategory
, CheckboxPreference
和android:summary
大小。 這些標簽沒有任何android:textSize
屬性。 任何人都可以幫我解決這個問題嗎?
最后,我能夠通過傳遞一個布局來解決我的問題。 也許這對某人有幫助。 這是我修改過的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>
請問我是否有任何疑問....繼續微笑。 如果這很有用,請給我一個評論。
對我來說,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的非全息版本。
我修改了Kumar對ICS及以上的答案。 我將此布局放在值-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>
您實際上可以看一下布局文件夾中特定平台中使用的布局:
<android-sdk>\platforms\android-<level>\data\res\layout
首選項布局以preference_前綴開頭。
看看我對此的回答: Android PreferenceScreen標題分為兩行 。
如果需要,您還可以更改字體本身。 只需添加:
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);
}
只是對布局代碼的一個小修改。 “@ + 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.