Android studio设置Button圆角化

Button圆角化

新建三个xml文件,分别命名为btn_normal.xml,btn_press.xml,btn_shape.xml,并将三个xml文件从layout目录下移动至drawable目录下。
新建xml文件步骤
将鼠标移至新建好的xml文件处,单击右键,按如下步骤进行操作。
移动xml文件步骤
将此处layout改为drawable。
移动至drawable
btn_normal.xml为普通样式,代码如下所示

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--对按钮进行圆角化-->
    <corners android:radius="10dp"/>
    <!--设置按钮背景颜色-->
    <solid android:color="#00000000"/>
    <!--stroke是按钮边框的线条-->
    <stroke
        android:width="1dp"
        android:color="#000000"/>
</shape>

btn_press.xml为点击样式,代码如下所示

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--对按钮进行圆角化-->
    <corners android:radius="10dp"/>
    <!--设置按钮背景颜色-->
    <solid android:color="#0aa4c7"/>
    <!--stroke是按钮边框的实现,每段虚线的长度,和两段虚线之间的颜色和虚线颜色-->
    <stroke
        android:width="1dp"
        android:dashWidth="8dp"
        android:dashGap="4dp"
        android:color="#000000"/>
</shape>

设置圆角大小,数值越大,形状越圆滑。
设置背景颜色,
btn_shape.xml为调用时的样式,代码如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--普通时候的样式-->
    <item android:drawable="@drawable/btn_normal" android:state_pressed="false"></item>
    <!--点击后的样式-->
    <item android:drawable="@drawable/btn_press" android:state_pressed="true"/>
</selector>

此时,该背景设置已完成,接下来Button调用该背景,在布局下放置Button控件,在控件Button中添加代码
android:background=“@drawable/btn_shape”,如下所示

 <Button
        android:layout_width="285dp"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="20dp"
        android:background="@drawable/btn_shape"
        android:id="@+id/bt"
        android:layout_gravity="center_horizontal" />

Button背景效果如下图所示
普通样式
点击时样式

  • 13
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Run

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值