Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式
先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下:
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
其中com.android.internal.R.attr.buttonStyle就是我们修改样式的关键了,网上的教程的修改方法大都是:
<Button
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:text="价格" />
也就是在对应的xml里面button控件里面编写style达到目的。
但是如果我们的app需要完全统一整个应用的button的样式,那么就需要在每一个button里面添加style。
这显然效率太低下了。
接下来打开我们项目中values文件夹下面的styles.xml文件,我们创建安卓项目的时候,会有一个默认的styles文件。
打开之后找到这段代码:
<style name="AppBaseTheme" parent="Theme.Holo.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
不保证读者的默认styles.xml和我的是一样的,不过大概是这个样子,有可能读者的最低支持是2.3、那么就没有Them.Light。
我们使用eclipse的快捷键打开这个Theme.Holo.Light。可以看到如下代码:
<style name="Theme.Holo.Light" parent="Theme.Light">
<item name="colorForeground">@android:color/bright_foreground_holo_light</item>
<item name="colorForegroundInverse">@android:color/bright_foreground_inverse_holo_light</item>
<item name="colorBackground">@android:color/background_holo_light</item>
<item name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_holo_light</item>
<item name="disabledAlpha">0.5</item>
<item name="backgroundDimAmount">0.6</item>
<!--此处省略大部分中间样式-->
<!-- Button styles -->
<item name="buttonStyle">@android:style/Widget.Holo.Light.Button</item> <item name="buttonStyleSmall">@android:style/Widget.Holo.Light.Button.Small</item>
<item name="buttonStyleInset">@android:style/Widget.Holo.Light.Button.Inset</item> <item name="buttonStyleToggle">@android:style/Widget.Holo.Light.Button.Toggle</item>
<item name="switchStyle">@android:style/Widget.Holo.Light.CompoundButton.Switch</item> <item name="selectableItemBackground">@android:drawable/item_background_holo_light</item>
<item name="borderlessButtonStyle">@android:style/Widget.Holo.Light.Button.Borderless</item>
<item name="homeAsUpIndicator">@android:drawable/ic_ab_back_holo_light</item>
从上面的代码,可以看到buttonStyle这个样式:
这个就是我们修改的关键了,如果读者有兴趣查看Holo主题的button样式是怎么编写的,可以自行查看,这里不是介绍的重点。
接下来开始定义我们自己的全局button的样式。
<resources>
<!-- Base application theme. -->
<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>
<item name="android:buttonStyle">@style/ButtonStyle</item>
</style>
<style name="ButtonStyle" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/comm_button_style</item>
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>
</resources>
我们在AppTheme 里面添加一个item,名字叫做android:buttonStyle,然后在下面编写我们要修改的butto的样式。
这里有一点需要注意的就是我们需要继承android:style/Widget.Button这个样式,因为如果不继承的话,我们就需要修改所有button的属性。而当前的示例中,我修改的只是background,其它属性我们照旧搬安卓本地主题的设置。
而且平时我们在编写界面的时候,对button设置了background之后,其实只是覆盖了系统默认button的其中一个样式而已,这点我们从button的源码可以看得到。
如果你不继承Widget.Button的话,那么出来的效果可能是面目全非的。
修改结果:

这种修改方式可以推广到其它的控件的修改,至于修改思路,可以参照上面介绍的button样式的修改方法。
原文:修改安卓默认的系统button样式,以及其它系统控件的默认样式
Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式的更多相关文章
- Vue——解决使用第三方组件库时无法修改默认样式的问题(使用 /deep/ )
最近在开发一个基于Vue的后台管理系统,其中使用了element-ui第三方ui组件库.使用过组件库的人都知道,第三方组件往往会有一些默认的样式,而有些又是我们想要改变的. 一.基础(了解 <s ...
- CSS3过渡结束监听事件,清除/修改表单元素的一些默认样式
document.querySelector('div').addEventListener('transitionEnd',function(){ console.log('过度结束') }) 如果 ...
- Android UI学习1:控件和基本事件的响应
在任何一个 GUI 系统中,控制界面上的控件(通常称为控件)都是一个基本的内容.对于 Android 应用程序,控件称为 View. 在 Android 中,在处理 UI 中的各种元素的时候,两个程序 ...
- (转载)Android UI设计之AlertDialog弹窗控件
Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...
- Android UI组件----ListView列表控件详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...
- 【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)
3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html Edi ...
- 【Android 界面效果18】Android软件开发之常用系统控件界面整理
[java] view plaincopyprint? <span style="font-size:18px">1.文本框TextView TextView的作用 ...
- Android软件开发之常用系统控件界面整理
1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android ...
随机推荐
- es6+最佳入门实践(5)
5.对象扩展 5.1.对象简写 在es5中,有这样一种写法 var name = "xiaoqiang"; var age = 12; var obj = { name : nam ...
- HDU1099---数学 | 思维
hdu 1099 Lottery题意:1~n编号的彩票,要买全,等概率条件下平均要买几张.已经买了m张时,买中剩下的概率为1-m/n,则要买的张数为1/(1-m/n)n=2,s=1+1/(1-1/2) ...
- mysql 基本操作练习
), sex ), age )); , '北京'); , '上海'); , '广州'); , '北京'); , '北京'); , '上海'); , '北京'); #(). 写出sql语句,查询所有年龄 ...
- Python基础(4)_集合、布尔类型
一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...
- bzoj3669: [Noi2014]魔法森林 lct版
先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...
- HoneyPy 模拟Nginx服务器脚本
HoneyPy是一个Python写的低交互式蜜罐,可以通过自定义Plugins的方式来配置不同的场景.这里是一个模拟Nginx空白页面的代码: # Auth xiaoxiaoleo # http:// ...
- 如何优雅地使用minicom
minicom简介 安装 minicom是linux下一款常用的串口调试工具.ubuntu环境下,使用如下命令安装 sudo apt-get install minicom 配置 使用前需要进行配置, ...
- 转载:超级强大的vim配置(vimplus)--续集
超级强大的vim配置(vimplus)--续集 原文地址:https://www.cnblogs.com/highway-9/p/5984285.html An automatic configura ...
- VC++/MFC中调用CHM帮助文档的方法
转载:http://blog.csdn.net/hediping9811/article/details/23341387 (1)用Word编辑好帮助文档,并保存为网页格式,如mhtml格式. (2) ...
- linux下rm命令删除文件名中包含特殊字符的文件【转】
转自:http://blog.itpub.net/143526/viewspace-1060083/ 1. 删除带“-”的文件名的方法 2. 删除包含其它特殊字符的文件 3. 删除系统打不出的乱码文件 ...