You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Android XML中为CardView内的Button添加右侧箭头?

实现带右侧箭头的Button(XML方案)

嘿,这事儿完全可以用XML实现!我给你几个简单的方案,你挑合适的来用:

方案1:直接给Button添加右侧图标

这是最快捷的方法,利用Button自带的drawableRight属性就能搞定,步骤如下:

  1. 先准备一个箭头图标(推荐用矢量图标,适配各种分辨率):在Android Studio里右键res/drawableNewVector Asset,选择Material Icons里的arrow_right即可。
  2. 修改你的Button代码,添加两个关键属性:
<Button 
    android:id="@+id/signup" 
    style="@style/Widget.AppCompat.Button.Borderless" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/background_light" 
    android:minWidth="0dp" 
    android:paddingEnd="16dp" <!-- 加大右侧内边距,让箭头不贴边缘 -->
    android:paddingStart="8dp"
    android:drawableRight="@drawable/ic_arrow_right" <!-- 绑定箭头图标 -->
    android:drawablePadding="8dp" <!-- 调整文本和箭头的间距 -->
    android:gravity="center_vertical|start" <!-- 让文本左对齐,箭头靠右 -->
    android:text="@string/signUp" 
    android:textColor="#DE000000" />

这样修改后,按钮就会显示你想要的右侧箭头效果了。

方案2:用TextView替代Button(更灵活)

如果Button的样式限制了你,比如想更自由调整箭头大小、位置,可以用TextView来模拟按钮,体验和Button完全一致:

<TextView
    android:id="@+id/signup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:paddingStart="8dp"
    android:paddingEnd="16dp"
    android:drawableRight="@drawable/ic_arrow_right"
    android:drawablePadding="8dp"
    android:gravity="center_vertical|start"
    android:text="@string/signUp"
    android:textColor="#DE000000"
    android:clickable="true"
    android:focusable="true"
    android:background="?attr/selectableItemBackground" <!-- 添加点击波纹效果 -->
    />

TextView设置clickable="true"focusable="true"后就能响应点击,加上?attr/selectableItemBackground还能获得Material Design的点击波纹效果,和Button的交互体验没差别。

方案3:自定义背景Drawable(进阶需求)

如果需要箭头随按钮状态变化(比如按下时箭头变色),可以写一个Selector Drawable,把箭头和背景状态绑定。不过看你的需求,前面两个方案应该就足够啦。

修改后的完整XML代码参考:

<android.support.v7.widget.CardView 
    android:id="@+id/signupCard" 
    style="@style/CardView.Light" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginBottom="18dp" 
    android:layout_marginEnd="29dp" 
    android:layout_marginLeft="29dp" 
    android:layout_marginRight="29dp" 
    android:layout_marginStart="29dp" 
    android:layout_marginTop="198dp" 
    app:layout_constraintBottom_toTopOf="@+id/loginCard" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent">

    <Button 
        android:id="@+id/signup" 
        style="@style/Widget.AppCompat.Button.Borderless" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@android:color/background_light" 
        android:minWidth="0dp" 
        android:paddingEnd="16dp"
        android:paddingStart="8dp"
        android:drawableRight="@drawable/ic_arrow_right"
        android:drawablePadding="8dp"
        android:gravity="center_vertical|start"
        android:text="@string/signUp" 
        android:textColor="#DE000000" />

</android.support.v7.widget.CardView>

内容的提问来源于stack exchange,提问作者Talib

火山引擎 最新活动