自preferenceCategory标题 [英] Custom PreferenceCategory Headings

查看:1595
本文介绍了自preferenceCategory标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样定义一个简单的preference屏幕

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>

和这里是我的活动

public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.layout.preferences);
    }
}

我该如何申请我的自定义样式的 preferenceCategory 标题?

推荐答案

您应该看看<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/res/res/values/styles.xml;h=d7b654e49809cb97a35682754b1394af5c8bc88b;hb=HEAD#l595"><$c$c>$p$pference.Category风格:

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>

让我们来看看 preference_category.xml 文件:

<!-- 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"
/>

所以,你需要创建自定义主题扩展默认的Andr​​oid 主题并覆盖 listSeparatorTextViewStyle ListHeader 的风格。然后将此主题的活动,扩展 preferenceActivity

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.

第一,在 styles.xml 添加下一个code:

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>

然后的Andr​​oidManifest.xml 添加主题,以您的preference活性的研究:

Then in your AndroidManifest.xml add theme to your preference acitivity:

 <activity android:name=".MyPreferencesActivity" 
           android:theme="@style/Theme.Custom" 
           ... >
 ...
 </activity>

下面是截图:

这篇关于自preferenceCategory标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆