在開發中,經常要替換RatingBar,EditText,RadioButton,CheckBox等等控件的樣式,如何替換,相信開發的朋友都會,我就簡單帶過。
比如:一個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>
這是系統自帶樣式的效果:
如果項目需要中明確要替換別的樣式,那么
第一種方式: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來實現:
<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處?做為一個懶人,我不能忍受。
懶是技術發展的動力。
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>
是吧, 現在直接用的時候,樣式也是自定義的了,怎么做到的呢?
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應用主題的規則上。
關于全局應用主題的方法 , 總結一下:
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 , 以上就是偷懶的正確方式,有坑 , 但爬起來,并總結,就成了自己的東西。
如果這篇文章給你帶來了喜悅,請幫忙點贊,讓更多人能看到
如果有不正確的地方,希望大家幫忙指正