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

博主最近研究了下自定义控件的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。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值