文章目錄
- 前言
- 一、自定義對話框中的按鈕樣式
- 二、自定義ActionBar的背景顏色、文字樣式
- 三、修改狀態欄顏色
- 總結
前言
最近對安卓開發感興趣,于是從0開始學習,在學習了java非常基礎的課程之后,就通過閱讀郭大神的《第一行代碼》來進行APP的開發學習,
學習完前三章,并且剛好看完了《挪威的森林》這本苦澀憂郁的青春小說,感觸良多,
于是就想著做一款非常簡單的無互動無資訊傳遞的APP,用來介紹和這本小說相關的東西,包括書評、作者、同名電影等,
為了練手,APP中刻意使用了目前學到的開發內容,如Recycler View/ List View、一些經典布局和組件等,同時在開發程序中為了美觀也學習了如何螢屏適配以及自定義一些樣式,
在本文中,主要記錄本次開發程序中遇到的問題和解決方案,基本不包括原理介紹😂
這些解決方案是我在翻閱了多個博客和網頁之后,挑選出來簡單易實作,且經過實驗驗證可行的方案,應該可以放心使用~
有些會放參考原文,沒有放的就說明我當時忘了收藏…
歡迎到Github上查看原始碼:挪威的森林APP
放幾張APP效果圖:



在本文中,介紹幾種常用的自定義樣式方法,包括:
- 自定義AlertDialog中的按鈕樣式
- 自定義ActionBar的背景顏色、文字樣式
- 自定義手機狀態欄的顏色
一、自定義對話框中的按鈕樣式
在開發程序中發現,向AlertDialog創建的對話框添加Positive和Negative按鈕后,這個按鈕默認的樣式巨丑無比,背景色是奢華的暗紫色,字體顏色好像是黑色,顏控表示擱誰也不能忍,于是尋找自定義按鈕樣式的方法,
一開始查到的方法都是說在theme.xml檔案中添加style自定義AlertDialog樣式,就像下面這樣:
<style name="AlertDialogCustom" parent="@style/Theme.AppCompat.Dialog.Alert">
<item name="buttonBarPositiveButtonStyle">@style/positiveBtnStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/negativeBtnstyle</item>
</style>
<style name="positiveBtnStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/black</item>
<item name="android:background">@color/white</item>
</style>
<style name="negativeBtnstyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#999999</item>
<item name="android:background">@color/white</item>
</style>
然后在java代碼中通過下面的方式使用這個自定義樣式創建AlertDialog:
AlertDialog alertDialog_comment = new AlertDialog.Builder(new ContextThemeWrapper(v.getContext(), R.style.AlertDialogCustom))
但是我按照這個方法搞了好久也不能實作自定義功能,在positiveBtnStyle中定義了格式后,也不會使實際顯示的樣式發生改變,
在經過多方查找后,我找到了一個非常靠譜的方法:直接獲得AlertDialog實體的按鈕,并對獲得的按鈕使用set方法來設定文字、背景樣式
在Activity.java檔案中添加以下代碼(假設已經先定義了一個AlertDialog實體叫alertDialog):
// 獲取positive按鈕
Button pos_button = (Button)alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
// 設定positive按鈕樣式
pos_button.setBackground(ContextCompat.getDrawable(v.getContext(),R.color.transparent));
pos_button.setTextColor(ContextCompat.getColor(v.getContext(),R.color.white));
// 獲取negative按鈕
Button neg_button = (Button)alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
// 設定negative按鈕樣式
neg_button.setBackground(ContextCompat.getDrawable(v.getContext(),R.color.transparent));
neg_button.setTextColor(ContextCompat.getColor(v.getContext(),R.color.white));
這樣的操作可以使我們能分別設定positive及negative按鈕的樣式,非常方便!
在上面的代碼中,我將按鈕背景設定為了“透明”(R.color.transparent),按鈕中文字顏色設定為白色(因為我的對話框是暗色的),比較好看~效果如下:

至于我的對話框為啥是暗色的,我覺得應該是因為我在定義對話框實體時,使用了繼承自@style/Theme.AppCompat.Dialog.Alert的自定義style:
AlertDialog alertDialog_comment = new AlertDialog.Builder(new ContextThemeWrapper(v.getContext(), R.style.AlertDialogCustom))
這行代碼就表示創建的對話框使用R.style.AlertDialogCustom格式,該格式中我沒有放任何東西,只是繼承了@style/Theme.AppCompat.Dialog.Alert:
<style name="AlertDialogCustom" parent="@style/Theme.AppCompat.Dialog.Alert">
</style>
然鵝我并不知道為啥這樣就會變暗…
本節參考:Android原生AlertDialog修改標題,內容,按鈕顏色,字體大小等
二、自定義ActionBar的背景顏色、文字樣式
相比于對話框,ActionBar的樣式好搞多了,直接在theme中定義style就可以,它的坑主要是在style的parent怎么寫,因為這和app中activity繼承自AppCompatActivity還是Activity有關,比較混亂,我也沒有搞清楚,
但是下面這個style的寫法對應了activity繼承AppCompatActivity的開發,沒有問題,
下面的部分實作了設定ActionBar背景為黑色,并設定上面文字顏色為白色的功能,
我們在valus/theme.xml中添加:
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="background">@color/black</item>
<item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>
<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
<!-- <item name="android:textSize">@dimen/dp_20</item>-->
</style>
這樣我們自定義了ActionBar的style,下面需要把這個style添加到我們的APP總主題中,于是在總主題中(總主題的parent是DarkActionBar)添加ActionBar的自定義主題:
<style name="Theme.NorwegianWood" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
......
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="overlapAnchor">false</item>
</style>
上面的結果是經過多次查找和實驗修改得到的,東拼西湊實作出來,所以參考已經找不到了…
三、修改狀態欄顏色
網上有很多動態修改手機狀態欄顏色的文章,但都很復雜,這里提供一個我找到的封裝好的方法,原文在這里:Android開發技巧——設定系統狀態欄顏色
使用方法很簡單:
- 首先在build.gradle的dependences中引入封裝檔案:
implementation 'com.githang:status-bar-compat:latest.integration'
- 在每個activity對應的.java檔案中的onCreate()中添加修改狀態欄顏色的代碼:
StatusBarCompat.setStatusBarColor(this, Color.parseColor("#000000"), false);
setStatusBarColor的第一個引數是this代表當前活動,第二個引數是我們希望設定的狀態欄顏色,第三個引數代表引數2的顏色是否是淺色,如果是則填true,否則填false,當然,第三個引數也可以不填,因為作者實作了多個建構式,
總結
開發程序中遇到了很多小問題,也學到了很多新知識,這里再放幾個雖然小但實用的問題及解決方案鏈接,便于查閱,親測可用有效:
Android設定TextView點擊時變換顏色
TextView文字加下劃線的方法
Android實作點擊兩次回傳鍵退出應用程式
下一篇博客將著重介紹使用AndroidAutoSize解決適配問題,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/282328.html
標籤:其他
