Android自定义Button

先看效果
这里写图片描述
//只需要把button的背景设置为一个样式

 <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test"
        android:textColor="#FFFFFF"
        android:layout_marginTop="20dip"
        android:background="@drawable/a_bg"
        android:gravity="center"/>

再在drawable下新建一个selector类型的xml文件 命名为a_bg.xml

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

    <item android:drawable="@drawable/a_bg_1" android:state_pressed="true"/>
    <item android:drawable="@drawable/a_bg_1" android:state_focused="true"/>
    <item android:drawable="@drawable/a_bg_2"/>

</selector>

这里a_bg_1是按下的状态
a_bg_2是正常状态

然后新建shape类型的a_bg_1.xml和a_bg_2.xml
分别是
a_bg_1.xml

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

    <solid android:color="#cccccc" />//设置按钮正常状态的颜色

    <stroke
        android:width="1dip"
        android:color="#00000088" />//设置边框颜色 后面aa为透明度

    <corners
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />//设置4个方位边角的半径

</shape>

a_bg_2.xml

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

    <solid android:color="#222222" />//设置按钮正常状态的颜色
    <stroke
        android:width="3dip"
        android:color="#000000aa" />//设置边框颜色 后面aa为透明度

    <corners
         android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />//设置4个方位边角的半径

</shape>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值