在開發(fā)中,經(jīng)常要替換RatingBar,EditText,RadioButton,CheckBox等等控件的樣式,如何替換,相信開發(fā)的朋友都會,我就簡單帶過。
比如:一個(gè)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="我是復(fù)選框 未選中"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="我是復(fù)選框 已選中"/>
</LinearLayout>
這是系統(tǒng)自帶樣式的效果:

如果項(xiàng)目需要中明確要替換別的樣式,那么
第一種方式: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="我是復(fù)選框 未選中"/>
<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="我是復(fù)選框 已選中"/>
</LinearLayout>

ok,替換成功 ,
第二種寫法,和第一種一樣,但是是用style來實(shí)現(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="我是復(fù)選框 已選中"/>
到這里,并沒有結(jié)束 ,
這里有個(gè)痛點(diǎn),就是在每一個(gè)使用到該控件的位置,都要加上一些屬性來修改為自己想要的效果,如果我們在項(xiàng)目中大量用到checkBox,EditText,并且又要做成特定的樣式 , 那不是要寫N+N處?做為一個(gè)懶人,我不能忍受。
懶是技術(shù)發(fā)展的動(dòng)力。
ok , 先上代碼,不廢話,
還是這個(gè)配方
<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>

是吧, 現(xiàn)在直接用的時(shí)候,樣式也是自定義的了,怎么做到的呢?
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();
}
}
可以看到構(gòu)造方法2,傳入了checkboxStyle,這個(gè)就是CheckBox的默認(rèn)樣式 ,于是我們可以在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里面應(yīng)用一個(gè)主題
<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>
重寫了三個(gè)控件的樣式 , 看看預(yù)覽,

嗯,目前看來,一切ok , 但是真機(jī)跑起來,

EditText的樣式?jīng)]有應(yīng)用上去?為什么呢?
我猜測是因?yàn)槲业腅ditText在被轉(zhuǎn)成了AppCompatEditText,要不打個(gè)斷點(diǎn)看下?
先給控件加個(gè)id:
<EditText
android:id="@+id/editText"
android:hint="請輸入用戶名密碼"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

嗯,結(jié)果和我想的一樣,那么這和沒有應(yīng)用上自定義樣式有什么關(guān)系呢?看看:
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);
}
對,一個(gè)是com.android.internal.R.attr.editTextStyle,一個(gè)是R.attr.editTextStyle,解決的辦法就是:
<item name="editTextStyle">@style/EditTextSel</item>
去掉前面的android:
再看下運(yùn)行效果

嗯,可以了, 問題在于appCompatv7應(yīng)用主題的規(guī)則上。
關(guān)于全局應(yīng)用主題的方法 , 總結(jié)一下:
1.跳到控件源碼的構(gòu)造方法里面看默認(rèn)樣式名字比如editTextStyle,xxStyle
2.在style.xml里定義一個(gè)樣式,但一定要繼承自原有樣式,然后修改,例如:
<style name="EditTextSel" parent="Widget.AppCompat.EditText">
<item name="android:background">@drawable/edit_sel</item>
<item name="android:textColorHint">#fff</item>
</style>
3.將自定義樣式,應(yīng)用到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應(yīng)用到application上,例如:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
5.做完以上幾點(diǎn), 在該項(xiàng)目中運(yùn)用的控件就是你自定義的了,而無需處處加style=xxx去附加樣式
ok , 以上就是偷懶的正確方式,有坑 , 但爬起來,并總結(jié),就成了自己的東西。
如果這篇文章給你帶來了喜悅,請幫忙點(diǎn)贊,讓更多人能看到
如果有不正確的地方,希望大家?guī)兔χ刚?/p>