在上一篇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

python preference界面设置_偏好设置如何更改Preference的样式相关推荐

  1. python preference界面设置_Preference 实现设置界面

    Preference 能简便地实现设置界面,且主要优点是设置的值会自动通过 SharedPreferences 方式保存下来,可直接取用. 一.简易实现 res/xml/prefs.xml: < ...

  2. python库路径_如何设置本地python库目录/ PYTHONPATH?

    在尝试编写今天使用PIL的 Python脚本的过程中,我发现我似乎没有在我的本地计算机上使用它(OS X 10.5.8,默认2.5 Python安装). 所以我跑: easy_install --pr ...

  3. AndroidStudio_开发工具的设置_界面设置_字体设置_使用习惯设置_按钮设置等一些设置的介绍---Android原生开发工作笔记71

    这里说一下androidstudio的一些设置,很多是不常用的,用的时候,自己百度一下也很方便,这里仅仅说一下就可以了 可以通过上面settings打开设置 可以看到这个设置框 可以搜索比如搜font ...

  4. echarts 统计图周月切换_如何设置ECharts星期轴的样式

    calendar.dayLabel | Object 设置 ECharts 日历坐标系中星期标签的样式. calendar.dayLabel.show | boolean [ default: tru ...

  5. Android添加拍照功能,Android相机开发(二): 给相机加上偏好设置

    Android Camera Develop: add settings to camera app 概述 继上一篇实现了一个最简单的相机APP后,本篇主要介绍实现相机的各种偏好设置,比如分辨率.闪光 ...

  6. Anaconda3 偏好设置

    六. 偏好设置(更改环境配置,避免占据系统盘) (1)更换conda源 https://blog.csdn.net/zhayushui/article/details/80433768 https:/ ...

  7. dock模拟macos教程_如何设置macOS应用程序以最小化其Dock图标

    dock模拟macos教程 When you minimize a window in macOS, it goes to the right edge of your Dock by default ...

  8. macos安全性偏好设置_如何更改macOS系统偏好设置的布局

    macos安全性偏好设置 If you don't care for the way the System Preferences appear in macOS, you can change th ...

  9. mac系统如何进入系统偏好设置_如何更改macOS系统偏好设置的布局 | MOS86

    如果你没有 通常,当您打开"系统偏好设置"时,这或多或少是它们的显示方式(您自己的Mac) 你有没有使用家长控制?你甚至有打印机还是扫描仪?如果没有,那么你可能不会 There 首 ...

最新文章

  1. SELinux与强制访问控制系统应用
  2. 李开复写给中国学生的七封信之给中国学生的第五封信——你有选择的权利(完)...
  3. 创建linux启动盘,linux系统中如何创建windows启动盘的详细介绍
  4. Vue打包后部署使用Nginx
  5. Mongo服务器二进制文件修复,Mongodb-File-Server
  6. 来来来!一次搞定各种数据库 SQL 执行计划:MySQL、Oracle
  7. 数学建模学习笔记(八)——分类模型
  8. kudu建表:Not enough live tablet servers to create a table with the requested replication factor 3. 2
  9. python切片表达式3个参数_Python:Base3(函数,切片,迭代,列表生成式)
  10. IDEA 修改文件编码
  11. 家庭生涯妙招,必定要看哦
  12. STM32(八)W25Q(16/32/64/128)芯片学习总结
  13. Vosk可以用于Unity的离线语音识别
  14. PHP PDF内容识别 抓取信息 方法
  15. css_使div中的文本自动换行
  16. STUFF()函数将查询的多行数据合并为一行
  17. matlab三点确定抛物线,曲线拟合 – 如何计算抛物线的顶点给定三点
  18. uni-app踩坑记录
  19. 电脑开机黑屏一串英文_电脑开机黑屏一串英文字母是为什么 开不了机
  20. Unity UDP传输图片

热门文章

  1. SAP Kyma GraphQL storage实现
  2. Enterprise search result view column显示technical name
  3. ABAP和XML数据格式互相转换的两种方式
  4. 计算机办公高级试题,高级办公软件试题及解答
  5. java实现聊天室界面javafx_java 聊天室WeChat
  6. python判断字符串是纯数字_python判断字符串是否纯数字的方法
  7. gin框架长连接_一个TCP长连接设备管理后台工程(一)
  8. mysql 递归查找父节点_MYSQL递归查询,根据子类ID查询所有父类(最全)
  9. python hacklib_【入门】angr:基于python的二进制分析框架
  10. 农村信用社招聘考试计算机,农村信用社招聘考试题:计算机(一)