Android开发--圆角 Button 按钮 xml 实现

现在的按钮都流行用圆角,所以今天上传下这方面的代码:
这里写图片描述
实现思路
shape 实现 圆角
在 drawable 新建两个 xml 文件, 这两个 xml 文件用 shape 实现了圆角效果.

Note:
因为要让用户有按下去的效果体验, 所有要有两套圆角图, 在按下去时候切换

<!-- res/drawable/button_shape_normal.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" >
<!-- rounded corner -->
<corners android:radius="5dp" />
<solid android:color="@color/orange_normal" />
</shape>
<!-- res/drawable/button_shape_pressed.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" >
<!-- rounded corner -->
<corners android:radius="5dp" />
<!-- fill with two colors, and the two colors change softly -->
<!--
         <gradient
        android:angle="270"
        android:startColor="@color/orange_light"
        android:endColor="@color/orange_normal"
        android:useLevel="true" />
    -->
<solid android:color="@color/orange_light" />
</shape>

selector 实现 按下 效果
在 drawable 新建 一个 xml 文件, 用 selector 把刚才 建立的两个 xml 文件组织起来

<!-- res/drawable/button_shape.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/button_shape_normal" android:state_pressed="false"/>
<item android:drawable="@drawable/button_shape_pressed" android:state_pressed="true"/>
</selector>

style 实现多个按钮复用

在 res/values/styles.xml 里面输入下面代码

<style name="SmsButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">20sp</item>
        <item name="android:background">@drawable/button_shape</item>
        <item name="android:textColor">@color/white</item>
    </style>

引用圆角按钮

在 layout/fragment_bomb.xml 里面 用 style 引用

 <Button
            android:id="@+id/fireup"
            style="@style/SmsButton"
            android:layout_marginEnd="20dp"
            android:layout_marginStart="20dp"
            android:text="@string/fireup" />

可以省去 建立 style 那步骤, 直接在 layout 里面引用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值