java按钮drawableStart在背景边框/角的外部绘制
我想做一个有点圆形的按钮,有一个可拉伸的开始(没有填充)
我在按钮背景中添加了以下内容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:shape="rectangle">
<solid 安卓:color="#CC232323"/>
<corners 安卓:radius="50sp"/>
</shape>
按钮是:
<Button
安卓:id="@+id/btn_play"
安卓:layout_width="match_parent"
安卓:layout_height="50sp"
安卓:layout_marginStart="25sp"
安卓:layout_marginEnd="25sp"
安卓:layout_weight="1"
安卓:background="@drawable/btn_bg"
安卓:drawableStart="@drawable/icon_layer_list"
安卓:text="@string/sample_text"
安卓:textAlignment="textStart"
安卓:textAllCaps="false"
安卓:textColor="@color/colorWhite" />
图层列表为:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
<item
安卓:drawable="@drawable/background_design"
安卓:height="50sp"
安卓:width="50sp"
/>
</layer-list>
角点确实作用于按钮本身,但drawableStart不受其影响,因此我得到一个按钮,一边是圆形边框,另一边是drawableStart的方形边框
# 1 楼答案
我找到了一个简单易行的解决方案,它的想法是用一个
CardView
来包装按钮,并将它的cardCornersRadius
设置为想要的半径像这样:
毫无疑问,这是我找到的最简单的方法,为了让视图在边界上运行,
CardView
不会让按钮或其中的任何东西在其边界/角之外绘制有关
CardView
(它的属性、依赖项等)的更多信息,转到This链接# 2 楼答案
视图(或您的案例中的按钮)始终为矩形。应用圆角背景时,它只是背景图像圆角,而不是视图本身。如果需要在drawableStart中设置圆角,则需要在为drawableStart设置的drawableStart中设置圆角