偏好设置如何更改Preference的样式

在上一篇Android设置界面5分钟搞定--Preferences的使用里讲到了如何运用PreferenceActivity快速的新建出偏好设置页面。如果是内部应用,这样丢过去是完全没有问题的,但我们日常用是达不到产品要求的。本篇我们就来看一下如何更改Preference的样式。

如何更改Preferences的样式

在android ui的开发中,要更改一个控件的样式,我们往往从这几个方面来做

  • 通过控件属性,如background,textSize
  • 通过指定控件的样式 style
  • 设置主题theme
    本来so easy的事情,但我惊奇的发现居然无法设置之前那些常用控件属性及样式来搞,只有主题控件能搞定。我们常用的三板斧只有Theme一招有效了。
    通过度娘大多讲的都是通过自定义来实现的,自定义除了每种子控件都要重写,感觉与自己写个布局差别不大了。
通过Theme来设置样式
  1. 在styles.xml里定义样式
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="CustomWindowTitleText" >
        <item name="android:textSize">20dip</item>
        <item name="android:textColor">#FFffffff</item>
        <item name="android:paddingLeft">10dp</item>
    </style>

    <style name="customCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/selector_checkbox</item>
    </style>


    <style name="Default.NoTitleBar" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:textColorPrimaryInverse">@android:color/black</item>
        <!--<item name="android:windowBackground">@color/window_bg</item>-->
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleSize">42.0dip</item>
        <item name="android:windowTitleStyle">@style/CustomWindowTitleText</item>
        <item name="android:checkboxStyle">@style/customCheckBox</item>
    </style>

    <style name="CustomWindowTitleBackground">
        <item name="android:background">#222222</item>
    </style>

    <!--这里是指定给PreferenceActivity的样式-->
    <style name="setStyle" parent="@style/Default.NoTitleBar">
        <item name="android:windowBackground">@drawable/wbg</item>                      <!-- 窗体背景   -->
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>           <!-- 窗体标题背景风格-->
        <item name="android:windowTitleSize">36dp</item>                        <!--窗体标题栏高度-->
        <!-- item name="android:listViewStyle"                          preference是一个LISTVIEW,这里设置该风格-->
        <item name="android:textColorPrimary">#ff0000</item>                        <!-- preference一级文本颜色-->
        <item name="android:textColorSecondary">#00ff00</item>                      <!--preference二级文本颜色-->
    </style>

</resources>

其中用到的setStyle是继承了Default.NoTitleBar,主要是为了在其它地方也可以使用,当然你也可以把属性都在写一起。

  1. 在AndroidManifest.xml里指定样式android:theme
<activity android:name=".MainActivity"
            android:theme="@style/setStyle">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

看下效果 (请原谅我的配色)


默认的效果
设置主题后的效果

各位,是不是很容易呢。

那下面放大招了
除了上面这种方式,其实我们可以通过指定布局来达到这个效果

通过布局实现样式的更改

通过设置android:layout ,android:widgetLayout
这2个属性通过布局文件来定义视图。
下面看一个CheckBoxReference的实现

  1. 新建两个布局文件
    preference_item.xml ,自由指定样式及布局吧,但请保持id与系统保持一致(CheckBoxReference对应的路径为frameworks/base/core/res/res/layout/preference_widget_checkbox.xml ,其它的自己找相应的就可以了)。
<?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:background="@drawable/selector_item"
    android:gravity="center_vertical"
    android:minHeight="?android:listPreferredItemHeight"
    android:orientation="horizontal" >

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="3dp"
        android:scaleType="fitStart"
        android:src="@drawable/appstore" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp" >

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:text="title"
            android:textColor="#4d4d4d"
            android:textSize="18.0sp" />

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@android:id/title"
            android:layout_toLeftOf="@android:id/widget_frame"
            android:maxLines="2"
            android:text="summary"
            android:textColor="#AAAAAA"
            android:textSize="14sp" />

        <LinearLayout
            android:id="@android:id/widget_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="4dp"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:orientation="vertical" >
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

checkbox_preference_widget.xml 用于设置checkbox

<?xml version="1.0" encoding="UTF-8"?>
<!-- 这里放上系统的id -->
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:button="@drawable/selector_checkbox"
    android:clickable="false"
    android:focusable="false"/>
  1. 给CheckBoxReference指定这两个属性,layout是布局文件(通用的),widgetLayout就是用于指定CheckBox的
<CheckBoxPreference
            android:layout="@layout/preference_item"
            android:icon="@mipmap/ic_launcher"
            android:key="parent_checkbox_preference"
            android:summary="选定后子控件可操作"
            android:title="父选择控件"android:widgetLayout="@layout/checkbox_preference_widget" />

就这两步,样式妥妥的了。

网上常用的自定义这里就不讲了,需要的可以谷哥。

本篇示例源码请移步github

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,204评论 0 17
  • 样式和主题(Styles and Themes) 一个样式(Style)是一个包含了指定样子和格式的作用于视图控件...
    张云飞Vir阅读 2,786评论 0 51
  • 胖嘟嘟肺炎终于好了,真是不容易。被哥哥传染感冒快速发展成为肺炎,期间吃了三天阿奇,打了三天阿奇,又吃了三天红霉素,...
    christal_sun阅读 232评论 0 0
  • 都说春天的风温柔地吹 那是妈妈的手 轻轻抚摸着你 可今天春天的风在怒吼 那是你积压多日 终于把你的心绪释放 清晨你...
    其乐绒绒阅读 233评论 0 0