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>
### 实现自定义Button样式 为了在 Android 中创建自定义按钮,通常会通过 XML 文件来定义按钮的不同状态下的外观。这可以通过 `shape` 和 `selector` 来完成。 #### 定义形状资源文件 首先,在项目的 `res/drawable` 目录下创建一个新的 XML 资源文件用于定义按钮的默认样式: ```xml <!-- res/drawable/button_shape.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置圆角半径 --> <corners android:radius="10dp"/> <!-- 添加渐变效果 --> <gradient android:startColor="#FFBB33" android:endColor="#FFFF44" android:angle="270"/> <!-- 边框设置 --> <stroke android:width="2dp" android:color="#87CEEB" /> </shape> ``` 此代码片段展示了如何利用 `<shape>` 标签及其子标签来自定义按钮的视觉特性[^3]。 #### 使用选择器定义多态样式 接着,同样是在 `res/drawable` 下建立另一个 XML 文件用来指定不同状态下应用哪种样式: ```xml <!-- res/drawable/btn_selector.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/button_shape_pressed"/> <!-- pressed state --> <item android:state_focused="true" android:drawable="@drawable/button_shape_focus"/> <!-- focused state --> <item android:drawable="@drawable/button_shape_default"/> <!-- default state --> </selector> ``` 这里使用了 `<selector>` 元素根据不同交互状态(按下、聚焦等)切换不同的背景图像或颜色[^4]。 #### 应用到布局中的 Button 组件 最后一步就是把上述定义好的可绘制对象应用于实际的 UI 布局里: ```xml <Button android:id="@+id/btn_click" android:text="启动" android:textSize="18sp" android:textColor="#9D9D9D" android:background="@drawable/btn_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" /> ``` 注意这里的 `android:background` 属性指向的是之前创建的选择器文件而不是直接使用的 shape 文件[^1]。 这样就完成了整个过程——从设计静态图形直到将其动态地绑定至应用程序界面上的一个按钮上去了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值