關于android中震叮,一步到位,統(tǒng)一系統(tǒng)控件樣式的一些看法

在開發(fā)中鳍鸵,經常要替換RatingBar,EditText,RadioButton,CheckBox等等控件的樣式苇瓣,如何替換,相信開發(fā)的朋友都會偿乖,我就簡單帶過击罪。
比如:一個CheckBox:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是復選框 未選中"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="我是復選框 已選中"/>

</LinearLayout>

這是系統(tǒng)自帶樣式的效果:

4517A0FD-4B32-4FB6-AD65-548481622708.png

如果項目需要中明確要替換別的樣式,那么
第一種方式:icon_checkbox_sel.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/icon_checkbox_unselect" android:state_checked="false"/>
    <item android:drawable="@drawable/icon_checkbox_select" android:state_checked="true"/>
</selector>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/icon_checkbox_sel"
        android:text="我是復選框 未選中"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:checked="true"
        android:button="@drawable/icon_checkbox_sel"
        android:text="我是復選框 已選中"/>

</LinearLayout>
448F68AE-E435-4893-B464-877AB2C7DBE1.png

ok贪薪,替換成功 外邓,
第二種寫法,和第一種一樣古掏,但是是用style來實現(xiàn):

<style name="CheckBoxSel" parent="Widget.AppCompat.CompoundButton.CheckBox">
        <item name="android:button">@drawable/icon_checkbox_sel</item>
    </style>
<CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:checked="true"
        style="@style/CheckBoxSel"
        android:text="我是復選框 已選中"/>

到這里损话,并沒有結束 ,
這里有個痛點槽唾,就是在每一個使用到該控件的位置丧枪,都要加上一些屬性來修改為自己想要的效果,如果我們在項目中大量用到checkBox,EditText,并且又要做成特定的樣式 庞萍, 那不是要寫N+N處拧烦?做為一個懶人,我不能忍受钝计。

懶是技術發(fā)展的動力恋博。

ok , 先上代碼,不廢話私恬,
還是這個配方

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我的樣式被全局定義 未選中"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:checked="true"
        android:text="我的樣式被全局定義 已選中"/>

</LinearLayout>
43BB9DB0-8D53-4824-81A3-C32CC1178EB2.png

是吧债沮, 現(xiàn)在直接用的時候,樣式也是自定義的了本鸣,怎么做到的呢疫衩?
1.看源碼,如果記不住的話:

public class CheckBox extends CompoundButton {
    public CheckBox(Context context) {
        this(context, null);
    }
    
    public CheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.checkboxStyle);
    }

    public CheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public CharSequence getAccessibilityClassName() {
        return CheckBox.class.getName();
    }
}

可以看到構造方法2荣德,傳入了checkboxStyle,這個就是CheckBox的默認樣式 闷煤,于是我們可以在style.xml里面

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:checkboxStyle">@style/CheckBoxSel</item>
    </style>
    <style name="CheckBoxSel" parent="Widget.AppCompat.CompoundButton.CheckBox">
        <item name="android:button">@drawable/icon_checkbox_sel</item>
    </style>

在AndroidManifest.xml的application里面應用一個主題

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

ok , 這樣就可以了, 再看看其他控件吧涮瞻,方法一樣:

    <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:checkboxStyle">@style/CheckBoxSel</item>
        <item name="android:ratingBarStyle">@style/RatingBarSel</item>
        <item name="android:editTextStyle">@style/EditTextSel</item>
    </style>

    <style name="CheckBoxSel" parent="Widget.AppCompat.CompoundButton.CheckBox">
        <item name="android:button">@drawable/icon_checkbox_sel</item>
    </style>

    <style name="EditTextSel" parent="Widget.AppCompat.EditText">
        <item name="android:background">@drawable/edit_sel</item>
        <item name="android:textColorHint">#fff</item>
    </style>

    <style name="RatingBarSel" parent="Widget.AppCompat.RatingBar">
        <item name="android:progressDrawable">@drawable/icon_rating_sel</item>
    </style>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我的樣式被全局定義 未選中"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:checked="true"
        android:text="我的樣式被全局定義 已選中"/>

    <android.support.v7.widget.AppCompatRatingBar
        android:layout_width="wrap_content"
        android:rating="2"
        android:numStars="5"
        android:stepSize="1"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content" />

    <EditText
        android:hint="請輸入用戶名密碼"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

重寫了三個控件的樣式 鲤拿, 看看預覽,

2E251580-3798-46BE-BA9A-8B366F445E4B.png

嗯署咽,目前看來近顷,一切ok , 但是真機跑起來,

C55B7727-4F33-4F35-B50B-23177AF4C2A2.png

EditText的樣式沒有應用上去?為什么呢幕庐?
我猜測是因為我的EditText在被轉成了AppCompatEditText,要不打個斷點看下家淤?
先給控件加個id:

<EditText
        android:id="@+id/editText"
        android:hint="請輸入用戶名密碼"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
F4F7C9DB-5136-4375-A0B4-2974B9F60F7E.png

嗯异剥,結果和我想的一樣,那么這和沒有應用上自定義樣式有什么關系呢絮重?看看:

public class EditText extends TextView {
    public EditText(Context context) {
        this(context, null);
    }

    public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }
public class AppCompatEditText extends EditText implements TintableBackgroundView {

    private AppCompatDrawableManager mDrawableManager;
    private AppCompatBackgroundHelper mBackgroundTintHelper;
    private AppCompatTextHelper mTextHelper;

    public AppCompatEditText(Context context) {
        this(context, null);
    }

    public AppCompatEditText(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.editTextStyle);
    }

對冤寿,一個是com.android.internal.R.attr.editTextStyle,一個是R.attr.editTextStyle,解決的辦法就是:

<item name="editTextStyle">@style/EditTextSel</item>

去掉前面的android:
再看下運行效果

02E55CD7-5248-4E26-882F-7D0448934C38.png

嗯青伤,可以了督怜, 問題在于appCompatv7應用主題的規(guī)則上。
關于全局應用主題的方法 狠角, 總結一下:

1.跳到控件源碼的構造方法里面看默認樣式名字比如editTextStyle,xxStyle
2.在style.xml里定義一個樣式号杠,但一定要繼承自原有樣式,然后修改丰歌,例如:
<style name="EditTextSel" parent="Widget.AppCompat.EditText">
        <item name="android:background">@drawable/edit_sel</item>
        <item name="android:textColorHint">#fff</item>
    </style>
3.將自定義樣式姨蟋,應用到AppTheme中去,例如:
<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:checkboxStyle">@style/CheckBoxSel</item>
        <item name="android:ratingBarStyle">@style/RatingBarSel</item>
        <item name="editTextStyle">@style/EditTextSel</item>
    </style>
4.將appTheme應用到application上,例如:
 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
5.做完以上幾點敷鸦, 在該項目中運用的控件就是你自定義的了特恬,而無需處處加style=xxx去附加樣式

ok , 以上就是偷懶的正確方式具滴,有坑 , 但爬起來堂飞,并總結,就成了自己的東西绑咱。

如果這篇文章給你帶來了喜悅绰筛,請幫忙點贊,讓更多人能看到
如果有不正確的地方描融,希望大家?guī)兔χ刚?/p>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末别智,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子稼稿,更是在濱河造成了極大的恐慌薄榛,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件让歼,死亡現(xiàn)場離奇詭異敞恋,居然都是意外死亡,警方通過查閱死者的電腦和手機谋右,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門硬猫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事啸蜜】友牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵衬横,是天一觀的道長裹粤。 經常有香客問我,道長蜂林,這世上最難降的妖魔是什么遥诉? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮噪叙,結果婚禮上矮锈,老公的妹妹穿的比我還像新娘。我一直安慰自己睁蕾,他們只是感情好苞笨,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著子眶,像睡著了一般猫缭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上壹店,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天猜丹,我揣著相機與錄音,去河邊找鬼硅卢。 笑死射窒,一個胖子當著我的面吹牛,可吹牛的內容都是我干的将塑。 我是一名探鬼主播脉顿,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼点寥!你這毒婦竟也來了艾疟?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敢辩,失蹤者是張志新(化名)和其女友劉穎蔽莱,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體戚长,經...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡盗冷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了同廉。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仪糖。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡柑司,死狀恐怖,靈堂內的尸體忽然破棺而出锅劝,到底是詐尸還是另有隱情攒驰,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布故爵,位于F島的核電站玻粪,受9級特大地震影響,放射性物質發(fā)生泄漏稠集。R本人自食惡果不足惜奶段,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一饥瓷、第九天 我趴在偏房一處隱蔽的房頂上張望剥纷。 院中可真熱鬧,春花似錦呢铆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至娜谊,卻和暖如春确买,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纱皆。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工湾趾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人派草。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓搀缠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親近迁。 傳聞我的和親對象是個殘疾皇子艺普,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內容