先介绍下修改原理:首先打开位于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控件的样式,以及其它系统控件的默认样式的更多相关文章

  1. Vue——解决使用第三方组件库时无法修改默认样式的问题(使用 /deep/ )

    最近在开发一个基于Vue的后台管理系统,其中使用了element-ui第三方ui组件库.使用过组件库的人都知道,第三方组件往往会有一些默认的样式,而有些又是我们想要改变的. 一.基础(了解 <s ...

  2. CSS3过渡结束监听事件,清除/修改表单元素的一些默认样式

    document.querySelector('div').addEventListener('transitionEnd',function(){ console.log('过度结束') }) 如果 ...

  3. Android UI学习1:控件和基本事件的响应

    在任何一个 GUI 系统中,控制界面上的控件(通常称为控件)都是一个基本的内容.对于 Android 应用程序,控件称为 View. 在 Android 中,在处理 UI 中的各种元素的时候,两个程序 ...

  4. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  5. Android UI组件----ListView列表控件详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  6. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)

    3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...

  7. 【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)

    3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html Edi ...

  8. 【Android 界面效果18】Android软件开发之常用系统控件界面整理

    [java] view plaincopyprint?   <span style="font-size:18px">1.文本框TextView TextView的作用 ...

  9. Android软件开发之常用系统控件界面整理

    1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android ...

随机推荐

  1. es6+最佳入门实践(13)

    13.模块化 13.1.什么是模块化 模块化是一种处理复杂系统分解为更好的可管理模块的方式.通俗的讲就是把一个复杂的功能拆分成多个小功能,并且以一种良好的机制管理起来,这样就可以认为是模块化.就像作家 ...

  2. Sencha Touch2 -- 11.1:定义具有关联关系的模型

    在Sencha Touch2.0中,可以定义不同模型之间的关联关系.例如,在开发博客网站的时候,可以首先定义用户(User)模型,然后为用户定义文章(Article)模型.一个用户可以发表多篇文章,因 ...

  3. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑——倍增floyd

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  4. LeetCode 10 Regular Expression Match

    '.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...

  5. gcc 簡單操作

    gcc -c test.c 產出 test.o object file gcc -c test.c -o XXX 產出 XXX object file gcc test.c -o aaa 產出 aaa ...

  6. linux===linux在线模拟器汇总

    jslinux: http://bellard.org/jslinux/ 一个叫Fabrice Bellard的工程师使用 JavaScript 在浏览器上模拟出了一个 Linux 系统.没有图形化界 ...

  7. WScript.Shell对象的 run()和exec()函数使用详解

    WScript.Shell对象的 run()和exec()函数使用详解 http://blog.sina.com.cn/s/blog_6e14a2050102v47g.html   vbScript ...

  8. 下载Instagram的图片

    1.接口地址 https://www.instagram.com/p/shortcode/?__a=1 如何获取shortcode 比如说文章地址是https://www.instagram.com/ ...

  9. PYTHON设计模式学习(2):什么是设计模式

    第一章: 本章主要是讨论什么是面向对象,在对面向对象理解的基础上,再讨论一些高深的话题,比如:设计模式. 所以,本章有如下主题: 1)明白什么是面向对象编程. 在学习设计模式之前,最好对python在 ...

  10. 漫谈网络通信——从OSI网络模型到TCP/IP协议族

    OSI七层模型 OSI(Open System Interconnection,开放系统互联)七层网络模型成为开放式系统互联参考模型,是一个把网络通信在逻辑上的定义,也可以理解成为定义了通用的网络通信 ...