关于android中,一步到位,统一系统控件样式的一些看法

在开发中,经常要替换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>

这是系统自带样式的效果:

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来实现:

<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>
43BB9DB0-8D53-4824-81A3-C32CC1178EB2.png

是吧, 现在直接用的时候,样式也是自定义的了,怎么做到的呢?
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应用主题的规则上。
关于全局应用主题的方法 , 总结一下:

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 , 以上就是偷懒的正确方式,有坑 , 但爬起来,并总结,就成了自己的东西。

如果这篇文章给你带来了喜悦,请帮忙点赞,让更多人能看到
如果有不正确的地方,希望大家帮忙指正

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容