zoukankan      html  css  js  c++  java
  • 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样式,以及其它系统控件的默认样式

  • 相关阅读:
    iOS:不同属性声明方式的解析
    iOS:图像和点击事件
    iOS:NAV+TABLE结合
    iOS:实现表格填充和选择操作
    iOS: 填充数据表格
    iOS:导航栏的工具条和导航条
    iOS:使用导航栏
    hello,world不使用ARC
    iOS代码实现:创建按钮,绑定按钮事件,读取控件值
    iOS版 hello,world版本2
  • 原文地址:https://www.cnblogs.com/H-BolinBlog/p/5972077.html
Copyright © 2011-2022 走看看