I have a simple preference screen defined like this
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Security"> <CheckBoxPreference android:title="Require Pin on Start" android:summary="Require pin to run the application" android:key="@string/pref_require_pin" android:defaultValue="false" /> </PreferenceCategory> <PreferenceCategory android:title="Settings"> <ListPreference android:title="History Age (in days)" android:summary="Display items up to 30 days old" android:key="@string/pref_history_days" android:defaultValue="30" android:entries="@array/days_list" android:entryValues="@array/days_list" android:dialogTitle="Select History Age"/> </PreferenceCategory> </PreferenceScreen>
I have a style setup already and used elsewhere in my app.
<style name="ListHeader"> <item name="android:textColor">#000000</item> <item name="android:textStyle">bold</item> <item name="android:textSize">12sp</item> <item name="android:background">#cccccc</item> <item name="android:paddingTop">6px</item> <item name="android:paddingBottom">6px</item> <item name="android:paddingLeft">12px</item> </style>
and here is my activity
public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.layout.preferences); } }
How do I apply my custom style to the PreferenceCategory heading?
解决方案
You should take a look at Preference.Category style:
<style name="Preference.Category"> <item name="android:layout">@android:layout/preference_category</item> <item name="android:shouldDisableView">false</item> <item name="android:selectable">false</item> </style>
Let's take a look at preference_category.xml file:
<!-- Layout used for PreferenceCategory in a PreferenceActivity. --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" style="?android:attr/listSeparatorTextViewStyle" android:id="@+android:id/title" />
So you need to create custom theme that extends default android Theme and override listSeparatorTextViewStyle value with ListHeader style. And then apply this theme to Activity that extends PreferenceActivity .
Here is how you can do it.
First, in your styles.xml add next code:
<style name="PreferenceListHeader" parent="@android:style/Widget.TextView.ListSeparator"> <item name="android:textColor">#000000</item> <item name="android:textStyle">bold</item> <item name="android:textSize">12sp</item> <item name="android:background">#cccccc</item> <item name="android:paddingTop">6px</item> <item name="android:paddingBottom">6px</item> <item name="android:paddingLeft">12px</item> </style> <style name="Theme.Custom" parent="@android:style/Theme"> <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item> </style>
Then in your AndroidManifest.xml add theme to your preference acitivity:
<activity android:name=".MyPreferencesActivity" android:theme="@style/Theme.Custom" ... > ... </activity>
Here is a screenshot:
我有这样定义一个简单的preference屏幕
&LT; preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android&GT; &LT; preferenceCategory机器人:标题=安全&GT; &LT;复选框preference 机器人:标题=需要输入PIN的开始 机器人:总结=需要输入PIN来运行应用程序 机器人:关键=@字符串/ pref_require_pin 安卓:设置defaultValue =FALSE/&GT; &LT; / preferenceCategory&GT; &LT; preferenceCategory机器人:标题=设置&GT; &LT;表preference 机器人:标题=历史年龄(天) 机器人:总结=显示项目达30天之久 机器人:关键=@字符串/ pref_history_days 安卓:设置defaultValue =30 机器人:项=@阵列/ days_list 机器人:entryValues =@阵列/ days_list 机器人:dialogTitle =选择历史年龄/&GT; &LT; / preferenceCategory&GT; &LT; / preferenceScreen&GT;
我已经一个样式的设置和我的应用程序在其他地方使用。
&LT;样式名称=ListHeader&GT; &LT;项目名称=机器人:文字颜色&GT;#000000&LT; /项目&GT; &LT;项目名称=机器人:TEXTSTYLE&GT;大胆&LT; /项目&GT; &LT;项目名称=机器人:TEXTSIZE&GT; 12SP&LT; /项目&GT; &LT;项目名称=机器人:背景&GT;#CCCCCC&LT; /项目&GT; &LT;项目名称=机器人:paddingTop&GT; 6px&LT; /项目&GT; &LT;项目名称=机器人:paddingBottom会&GT; 6px&LT; /项目&GT; &LT;项目名称=机器人:以下属性来&GT; 12px的&LT; /项目&GT; &LT; /风格&GT;
和这里是我的活动
公共类preferencesActivity扩展preferenceActivity实现OnShared preferenceChangeListener { @覆盖 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 加preferencesFromResource(R.layout preferences。); } }
我该如何申请我的自定义样式的 preferenceCategory 标题?
解决方案
您应该看看$p$pference.Category$c$c>风格:
&LT;样式名称=preference.Category&GT; &LT;项目名称=机器人:布局&GT; @android:布局/ preference_category&LT; /项目&GT; &LT;项目名称=机器人:shouldDisableView&GT;假&LT; /项目&GT; &LT;项目名称=机器人:选择&GT;假&LT; /项目&GT; &LT; /风格&GT;
让我们来看看 preference_category.xml 文件:
&LT;! - 在preferenceActivity用于preferenceCategory布局。 - &GT; &LT; TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android 风格=机器人:ATTR / listSeparatorTextViewStyle 机器人:ID =@ +安卓ID /标题 /&GT;
所以,你需要创建自定义主题扩展默认的Android 主题并覆盖 listSeparatorTextViewStyle 值 ListHeader 的风格。然后将此主题的活动,扩展 preferenceActivity 。
下面是你如何能做到这一点。
第一,在 styles.xml 添加下一个code:
&LT;样式名称=preferenceListHeader 父=@安卓风格/ Widget.TextView.ListSeparator&GT; &LT;项目名称=机器人:文字颜色&GT;#000000&LT; /项目&GT; &LT;项目名称=机器人:TEXTSTYLE&GT;大胆&LT; /项目&GT; &LT;项目名称=机器人:TEXTSIZE&GT; 12SP&LT; /项目&GT; &LT;项目名称=机器人:背景&GT;#CCCCCC&LT; /项目&GT; &LT;项目名称=机器人:paddingTop&GT; 6px&LT; /项目&GT; &LT;项目名称=机器人:paddingBottom会&GT; 6px&LT; /项目&GT; &LT;项目名称=机器人:以下属性来&GT; 12px的&LT; /项目&GT; &LT; /风格&GT; &LT;样式名称=Theme.Custom父=@安卓风格/主题&GT; &LT;项目名称=机器人:listSeparatorTextViewStyle&GT; @风格/ preferenceListHeader&LT; /项目&GT; &LT; /风格&GT;
然后在的AndroidManifest.xml 添加主题,以您的preference活性的研究:
&LT;活动机器人:名称=。我的preferencesActivity 机器人:主题=@风格/ Theme.Custom ...&GT; ... &LT; /活性GT;
下面是截图: