android提高——自定义控件(button)
2013-10-30 21:28
281 查看
博主最近研究了下自定义控件的button相关的内容,觉得不做下笔记对不起自己,就怕过些天就淡忘了,就和大家分享下:
一、自定义出来的效果如下:
效果:三个按键,正常状态是粉红色的背景,黑色字体;当button被按下时,背景变为蓝色,字体变为红色。
二、实现的步骤如下:
1、字体颜色改变的实现:
1)创建文件:
2)在所定义的button按键添加如下属性:
完成以上两步即可实现按键在不同状态字体的颜色变化。
具体文档请参考:http://developer.android.com/guide/topics/resources/color-list-resource.html
下面再实现背景的切换。
2、button背景改变的实现(与1略有不同):
1)、先在res/value目录下定义专门存放一个颜色值的文件:colors.xml
2)、创建文件
3)在所定义的button按键添加如下背景属性:
3、提供下效果图的布局文件:
参考文档:http://developer.android.com/guide/topics/resources/drawable-resource.html
完成以上步骤,大功告成~
备注:在color和drawable文件夹定义的只有文件名称会生成在R文件中生成相应的id;而在value文件夹定义的,每个节点都会在R文件生成相应的id。
一、自定义出来的效果如下:
效果:三个按键,正常状态是粉红色的背景,黑色字体;当button被按下时,背景变为蓝色,字体变为红色。
二、实现的步骤如下:
1、字体颜色改变的实现:
1)创建文件:
res/color/text_clr.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed red--> <item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused bule--> <item android:color="#ff000000"/> <!-- default black--> </selector>
2)在所定义的button按键添加如下属性:
android:textColor="@color/text_clr"
完成以上两步即可实现按键在不同状态字体的颜色变化。
具体文档请参考:http://developer.android.com/guide/topics/resources/color-list-resource.html
下面再实现背景的切换。
2、button背景改变的实现(与1略有不同):
1)、先在res/value目录下定义专门存放一个颜色值的文件:colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="bk_press">#0ff</drawable> <!-- press pick--> <drawable name="bk_foucus">#f00</drawable> <!-- foucus bule--> <drawable name="bk_normal">#ECA8B3</drawable> <!-- default pick--> </resources>
2)、创建文件
res/drawable/button_bg.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/bk_press" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/bk_foucus" /> <!-- focused --> <item android:drawable="@drawable/bk_normal" /> <!-- default --> </selector>
3)在所定义的button按键添加如下背景属性:
android:background="@drawable/button_bg"
3、提供下效果图的布局文件:
<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:gravity="center_vertical"> <Button android:id="@+id/bt1" android:layout_marginTop="5dip" android:layout_centerHorizontal="true" android:text="@string/but1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/text_clr" android:background="@drawable/button_bg" /> <Button android:id="@+id/bt2" android:layout_marginTop="5dip" android:layout_centerHorizontal="true" android:text="@string/but2" android:layout_below="@id/bt1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/text_clr" android:background="@drawable/button_bg" /> <Button android:id="@+id/bt3" android:layout_marginTop="5dip" android:layout_centerHorizontal="true" android:text="@string/but3" android:layout_below="@id/bt2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/text_clr" android:background="@drawable/button_bg" /> </RelativeLayout>
参考文档:http://developer.android.com/guide/topics/resources/drawable-resource.html
完成以上步骤,大功告成~
备注:在color和drawable文件夹定义的只有文件名称会生成在R文件中生成相应的id;而在value文件夹定义的,每个节点都会在R文件生成相应的id。
相关文章推荐
- [Android自定义控件]Android中如何用代码写出优美的Button
- android 自定义button背景图片及事件——自定义控件学习(一)
- android自定义控件SlidingButtonView实现类似QQ滑动删除效果
- android自定义控件之radioButton
- android自定义控件Button 带图片文字
- android自定义控件系列教程----真正的圆角button来了
- Android提高篇之listView点击button翻页功能实现
- android自定义控件,动态设置Button的样式
- android自定义控件,动态设置Button的样式
- android自定义控件,动态设置Button的样式
- android自定义控件,动态设置Button的样式
- Android开发之自定义控件-自定义Button
- Android自定义控件系列(一)—Button七十二变
- android自定义控件,动态设置Button的样式
- Android自定义控件(二):提高篇
- 【android自定义控件】button样式自定义<二>
- android动画入门,进阶,提高----自定义控件绘图篇和Path之贝赛尔曲线
- Android自定义控件制作显示进度的Button
- Android自定义控件系列(一)—Button七十二变
- Android 自定义控件 EditText输入框两边加减按钮Button