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就是我們修改樣式的關鍵了,網上的教程的修改方法大都是:
<Buttonstyle="@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 inres/values-vXX/styles.xml, while customizations related tobackward-compatibility can go here.--></style><!-- Application theme. --><style name="AppTheme" parent="AppBaseTheme">
不保證讀者的默認styles.xml和我的是一樣的,不過大概是這個樣子,有可能讀者的最低支持是2.3、那么就沒有Them.Light。
我們使用eclipse的快捷鍵打開這個Theme.Holo.Light??梢钥吹饺缦麓a:
<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樣式,以及其它系統控件的默認樣式
?
轉載于:https://www.cnblogs.com/H-BolinBlog/p/5972077.html
總結
以上是生活随笔為你收集整理的Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电影《天下无贼》的内容是?
- 下一篇: 乌镇停车场收费标准