今天给大家带来一个很简单但是很常用的控件ButtonExtendM,在开发中我们经常会用到图片加文字的组合控件,像这样:
以上图片都是从微信上截取的。(暂时没有找到icon在下,文字在上的例子)
下面我们通过一个控件来实现上下左右全部的样式,只需改动一个属性值即可改变icon的位置,是不是很方便,先看下demo效果图:
没错上图的三种不同的样式都是通过同一个控件实现的,下面我们看下代码
第一步 自定义属性
在res/values/目录下新建attrs.xml文件,
添加如下属性
<attr name="backColor" format="color" />
<attr name="backColorPress" format="color" />
<attr name="textColor" format="color" />
<attr name="textColorPress" format="color" />
<declare-styleable name="ButtonExtendM">
<attr name="backColor"/>
<attr name="backColorPress"/>
<attr name="textColor"/>
<attr name="textColorPress"/>
<attr name="iconDrawable" format="reference" />
<attr name="iconDrawablePress" format="reference" />
<attr name="text" format="string" />
<attr name="textSize" format="float" />
<attr name="spacing" format="dimension" />
<attr name="style">
<enum name="iconLeft" value="0" />
<enum name="iconRight" value="1" />
<enum name="iconUp" value="2" />
<enum name="iconBottom" value="3" />
</attr>
</declare-styleable>
第二步 新建布局文件view_button_extend_m.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
an