修改安卓默认的系统button样式,以及其它系统控件的默认样式

本文介绍了一种全局定制Android应用中Button样式的高效方法,通过在styles.xml文件中修改AppTheme来实现,避免了为每个Button单独设置样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    先介绍下修改原理:首先打开位于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的样式。

<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:buttonStyle">@style/ButtonStyle</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
   <style name="ButtonStyle" parent="@android:style/Widget.Button">
       <item name="android:background">@drawable/app_gray_radius_btn</item>
       
   </style>



    我们在AppTheme 里面添加一个item,名字叫做android:buttonStyle,然后在下面编写我们要修改的butto的样式。

    这里有一点需要注意的就是我们需要继承android:style/Widget.Button这个样式,因为如果不继承的话,我们就需要修改所有button的属性。而当前的示例中,我修改的只是background,其它属性我们照旧搬安卓本地主题的设置。
    而且平时我们在编写界面的时候,对button设置了background之后,其实只是覆盖了系统默认button的其中一个样式而已,这点我们从button的源码可以看得到。
    如果你不继承Widget.Button的话,那么出来的效果可能是面目全非的。

    附上两张效果图,一张是没有定义全局样式之前的,一张是定义了之后的。

可以看到确确实实是修改了,而且我并没有为每一个button编写android:style="xxxx".

这种修改方式可以推广到其它的控件的修改,至于修改思路,可以参照上面介绍的button样式的修改方法。


转载于:https://my.oschina.net/zxcholmes/blog/383552

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值