Android : 自定义带箭头的Button

本文详细介绍了如何使用XML创建圆角按钮样式,包括基本形状、点击状态和焦点状态的定义,以及如何在布局中应用这些样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

button


样式如下:

rounded_corner_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2dp" android:color="@color/lightgray" />
    <corners android:radius="10dp" />
    <solid android:color="@android:color/white"/>
</shape>

rounded_corner_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" >
        <shape>
            <stroke android:width="2dp" android:color="@color/orange" />
            <corners android:radius="10dp" />
            <solid android:color="@color/orange"/>
        </shape>
    </item>
    <item android:state_focused="true" android:drawable="@drawable/rounded_corner_shape" />
    <item android:drawable="@drawable/rounded_corner_shape" />
</selector>

style.xml

<resources>
    <style name="RoundedCornerView">
        <item name="android:background">@drawable/rounded_corner_shape</item>
        <item name="android:cacheColorHint">@color/transparent</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginBottom">5dp</item>
        <item name="android:paddingLeft">12dp</item>
        <item name="android:paddingRight">12dp</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="RoundedCornerBtn" parent="RoundedCornerView">
        <item name="android:background">@drawable/rounded_corner_btn_selector</item>
        <item name="android:gravity">left|center_vertical</item>
        <item name="android:textColor">@color/gray</item>
    </style>
    <style name="RoundedCornerBtnWithRightArrow" parent="RoundedCornerBtn">
        <item name="android:drawableRight">@drawable/right_arrow</item>
    </style>    
</resources>

在layout中为按钮设置样式

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="带箭头的按钮"
    android:textSize="20sp"
    style="@style/RoundedCornerBtnWithRightArrow"/>

参考文章:http://fokman.iteye.com/blog/1616831

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值