【最新版本已支持selector,经过代码生成drawable,详情查看 github.com/JavaNoober/…
关于如何进行预览,查看文章 无需自定义View,完全解放shape,selector(二)android
若是有什么问题,方便你们交流,建立了一个qq群,群号887686934,欢迎你们加入git
做为一个android程序员,对于shape、selector这两个标签必定不陌生。每当UI设计师给咱们设计出一个个button背景的时候,咱们就须要去drawable文件夹下去新建一个bg_xxx.xml,而后不少时候区别仅仅是一个边框的颜色或者填充的颜色。这就致使了不少很是类似的.xml文件产生。
网上以前也有了一种经过自定义View,在xml中经过设置属性达到shape效果的控件。可是这种自定义的控件不太灵活,归根究竟是一个自定义的button,若是我想改造项目的话就得去替换原有的button或者textView。接下来就给你们提供一种更加简单的方式:
无需自定义View,直接添加属性即可以实现shape、selector效果。程序员
话很少说,直接上代码。
从1.5.0版本开始,无需任何代码,直接加入标签便可,请使用最新版本
使用方法:
一、在BaseActiviy的super.onCreate()以前调用一行代码,仅仅是一个方法github
咱们来添加一些实例属性:app
咱们来看一下实际效果:框架
如今UI设计师告诉咱们要改一下背景,没事,咱们只须要在xml添加或者修改属性就行。
咱们来把圆形改为正方形,加个边框。5秒ok!布局
app:xxx属性就不用多说了,这些就是一些自定义属性而已。在这里我把shape、selector的部分属性转换成自定义的属性,这样就方便添加到已有原生控件中。post
这个方法是这个框架惟一须要加入的代码。
inject中其实是给LayoutInflater添加了一个LayoutInflater.Factory类。而Android的Activity在建立过程(也就是setContentView)中其实是经过把xml转换成View的对象。而LayoutInflater.Factory至关于这中间的一个后门,它是xml解析建立成View的必经方法,google中的v7support包里不少内容就是经过LayoutInflater.Factory来实现向下兼容的。
在这里,我经过低入侵的方式,加入一个自定义的LayoutInflater.Factory,去解析添加的自定义属性,接下来就简单了。生成系统提供的GradientDrawable、RippleDrawable、StateListDrawable便可。
同时因为AppcompatActivity是已经实现了LayoutInflater.Factory,而Activity又设定一个Activity只能加入一个factory类,所以这里须要在super.onCreate以前调用该方法,利用AppcompatActivity的factory去建立View。
具体原理解释能够参考个人这篇文章:Android 经常使用换肤方式以及原理分析this
添加依赖:google
implementation "com.android.support:appcompat-v7:$supportVersion"
implementation 'com.noober.backgorund:core:1.0.5'
复制代码
BaseActivity的super.onCreate以前添加代码:
BackgroundLibrary.inject(context);
复制代码
支持属性,命名规则就是标签名_标签属性名,支持shape全部属性:
<attr name="shape" format="enum">
<enum name="rectangle" value="0" />
<enum name="oval" value="1" />
<enum name="line" value="2" />
<enum name="ring" value="3" />
</attr>
<attr name="solid_color" format="color"/>
<attr name="corners_radius" format="dimension"/>
<attr name="corners_bottomLeftRadius" format="dimension"/>
<attr name="corners_bottomRightRadius" format="dimension"/>
<attr name="corners_topLeftRadius" format="dimension"/>
<attr name="corners_topRightRadius" format="dimension"/>
<attr name="gradient_angle" format="integer"/>
<attr name="gradient_centerX" format="float"/>
<attr name="gradient_centerY" format="float"/>
<attr name="gradient_centerColor" format="color"/>
<attr name="gradient_endColor" format="color"/>
<attr name="gradient_startColor" format="color"/>
<attr name="gradient_gradientRadius" format="dimension"/>
<attr name="gradient_type" format="enum">
<enum name="linear" value="0" />
<enum name="radial" value="1" />
<enum name="sweep" value="2" />
</attr>
<attr name="gradient_useLevel" format="boolean"/>
<attr name="padding_left" format="dimension"/>
<attr name="padding_top" format="dimension"/>
<attr name="padding_right" format="dimension"/>
<attr name="padding_bottom" format="dimension"/>
<attr name="size_width" format="dimension">
<enum name="wrap_content" value="-2" />
<enum name="match_parent" value="-1" />
</attr>
<attr name="size_height" format="dimension">
<enum name="wrap_content" value="-2" />
<enum name="match_parent" value="-1" />
</attr>
<attr name="stroke_width" format="dimension"/>
<attr name="stroke_color" format="color"/>
<attr name="stroke_dashWidth" format="dimension"/>
<attr name="stroke_dashGap" format="dimension"/>
<!--如下是selector事件-->
<attr name="ripple_enable" format="boolean"/>
<attr name="ripple_color" format="color"/>
<attr name="unpressed_color" format="color"/>
<attr name="pressed_color" format="color"/>
复制代码
例如加一个边框背景,以下便可:
<TextView
android:layout_width="130dp"
android:layout_height="36dp"
android:gravity="center"
android:text="TextView"
android:textColor="#8c6822"
android:textSize="20sp"
app:corners_radius="4dp"
app:solid_color="#E3B666"
app:stroke_color="#8c6822"
app:stroke_width="2dp" />
复制代码
若是须要selector效果,须要同时添加
app:unpressed_color
app:pressed_color
复制代码
若是须要水波纹效果,5.0以上才支持:
app:ripple_enable="true"//打开水波纹开关
app:solid_color="xxx"//设置默认填充颜色
app:ripple_color="xxx"//设置水波纹颜色
复制代码
注意:
一、若是直接给原生控件添加属性,在xml中会以下报红色异常,这时候不用理会便可。
tools:ignore="MissingPrefix"
复制代码
便可
项目地址:github.com/JavaNoober/…
后续更新完善内容会在项目里标明,你们的star是我继续研究的动力☺!
若是有什么问题,方便你们交流,建立了一个qq群,群号887686934,欢迎你们加入