您的位置:首页 > 移动开发 > Android开发

android提高——自定义控件(button)

2013-10-30 21:28 281 查看
博主最近研究了下自定义控件的button相关的内容,觉得不做下笔记对不起自己,就怕过些天就淡忘了,就和大家分享下:

一、自定义出来的效果如下:



效果:三个按键,正常状态是粉红色的背景,黑色字体;当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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息