在開發(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)自帶樣式的效果:
如果項目需要中明確要替換別的樣式,那么
第一種方式: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>
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>
是吧债沮, 現(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>
重寫了三個控件的樣式 鲤拿, 看看預覽,
嗯署咽,目前看來近顷,一切ok , 但是真機跑起來,
EditText的樣式沒有應用上去?為什么呢幕庐?
我猜測是因為我的EditText在被轉成了AppCompatEditText,要不打個斷點看下家淤?
先給控件加個id:
<EditText
android:id="@+id/editText"
android:hint="請輸入用戶名密碼"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
嗯异剥,結果和我想的一樣,那么這和沒有應用上自定義樣式有什么關系呢絮重?看看:
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:
再看下運行效果
嗯青伤,可以了督怜, 問題在于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>