Android自己定义控件皮肤

万事须己运,他得非我贤。这篇文章主要讲述Android自己定义控件皮肤相关的知识,希望能为你提供帮助。
android自己定义控件皮肤
对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观。甚至于很多button在按下时的外观都有一定变化,用户体验十分好。
这当中,就涉及到了Android自己定义控件属性的操作方法,下面操作以实现自己定义button皮肤为例。
1. 我们要自己定义将要实现的外观状态。能够是图片或者是自己定义的xml,这是我们直接自己定义不同状态的颜色xml,在values文件夹下新建colors.xml,代码例如以下:【Android自己定义控件皮肤】colors.xml

< ?xml version="1.0" encoding="utf-8"?> < resources> < color name="btn_no_pressed"> #3300ffff< /color> < color name="btn_pressed"> #33ffff00< /color> < /resources>

文件里的name能够依据项目须要自己定义命名,值是採用argb格式。
2. 我们要对将要实现的button不同状态进行设置,在drawable文件夹下新建btn_bg.xml文件,类型记得选择“selector”,代码例如以下:btn_bg.xml
< ?
xml version="1.0" encoding="utf-8"?
> < selector xmlns:android="http://schemas.android.com/apk/res/android" > < item android:state_pressed="false" android:drawable="@color/btn_no_pressed"> < /item> < item android:state_pressed="true" android:drawable="@color/btn_pressed"> < /item> < /selector>

android:state_pressed=""定义了控件是否被按下,值为boolean,类似的属性大家能够自行尝试学习,android:drawable=""定义了该控件处于对应状态时的外观。
3. 我们此时就能够将定义好的状态应用于布局文件里了,代码例如以下:activity_main.xml
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > < Button android:id="@+id/mybtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="click me" android:background="@drawable/btn_bg" /> < /RelativeLayout>

上述步骤做完之后。就能够将应用执行调试了,这仅仅是一个简单的样例> ~<

    推荐阅读