android添加控件selector样式

首先,在main.xml中添加一个按钮控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lg_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/bt_login"
            android:onClick="toLogin" />
    </LinearLayout>

在res目录下新建一个drawable文件夹,新建bt_login.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 控件获得焦点但是没有点击 -->
    <item android:drawable="@drawable/button_on" android:state_focused="true" android:state_pressed="false"></item>
    <!-- 控件没有获得焦点,也没有点击 -->
    <item android:drawable="@drawable/button_on" android:state_focused="false" android:state_pressed="false"></item>
    <!-- 控件点击状态 -->
    <item android:drawable="@drawable/button_down" android:state_pressed="true"></item>
    <!-- 控件选中状态 -->
    <item android:drawable="@drawable/button_down" android:state_checked="true"></item>

</selector>

main.xml中的

android:background="@drawable/bt_login"

这句代码引用了bt_login.xml文件,这就给这个按钮控件添加了一个selector样式

效果图如下:

这是登录界面!

 
第一张是默认状态,第二张是点击状态,颜色差别不是很大,所以截图效果不是很明显。

其实是两张不一样的图片,手机上就看的很明显。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值