在上一篇Android设置界面5分钟搞定--Preferences的使用里讲到了如何运用PreferenceActivity快速的新建出偏好设置页面。如果是内部应用,这样丢过去是完全没有问题的,但我们日常用是达不到产品要求的。本篇我们就来看一下如何更改Preference的样式。
如何更改Preferences的样式
在android ui的开发中,要更改一个控件的样式,我们往往从这几个方面来做
通过控件属性,如background,textSize等
通过指定控件的样式 style
设置主题theme
本来so easy的事情,但我惊奇的发现居然无法设置之前那些常用控件属性及样式来搞,只有主题控件能搞定。我们常用的三板斧只有Theme一招有效了。
通过度娘大多讲的都是通过自定义来实现的,自定义除了每种子控件都要重写,感觉与自己写个布局差别不大了。
通过Theme来设置样式
在styles.xml里定义样式
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
20dip
#FFffffff
10dp
@drawable/selector_checkbox
@android:color/black
@null
42.0dip
@style/CustomWindowTitleText
@style/customCheckBox
#222222
@drawable/wbg
@style/CustomWindowTitleBackground
36dp
#ff0000
#00ff00
其中用到的setStyle是继承了Default.NoTitleBar,主要是为了在其它地方也可以使用,当然你也可以把属性都在写一起。
在AndroidManifest.xml里指定样式android:theme
android:theme="@style/setStyle">
看下效果 (请原谅我的配色)
默认的效果
设置主题后的效果
各位,是不是很容易呢。
那下面放大招了
除了上面这种方式,其实我们可以通过指定布局来达到这个效果
通过布局实现样式的更改
通过设置android:layout ,android:widgetLayout
这2个属性通过布局文件来定义视图。
下面看一个CheckBoxReference的实现
新建两个布局文件
preference_item.xml ,自由指定样式及布局吧,但请保持id与系统保持一致(CheckBoxReference对应的路径为frameworks/base/core/res/res/layout/preference_widget_checkbox.xml ,其它的自己找相应的就可以了)。
<?xml version="1.0" encoding="UTF-8"?>
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" >
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" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp" >
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" />
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" />
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" >
checkbox_preference_widget.xml 用于设置checkbox
<?xml version="1.0" encoding="UTF-8"?>
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"/>
给CheckBoxReference指定这两个属性,layout是布局文件(通用的),widgetLayout就是用于指定CheckBox的
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