日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式

發布時間:2023/11/27 生活经验 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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。可以看到如下代碼:

<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控件的样式,以及其它系统控件的默认样式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。