[英]How to add stroke to just bottom side of Material button?
您不能将MaterialButton
用于这样的自定义drawable Background
,在它的文档中说:
不要使用
android:background
属性。MaterialButton
管理自己的background
drawable
,设置一个新的background
意味着MaterialButton
不能再保证它引入的新属性会正常工作。 如果更改默认background
,则MaterialButton
不能保证定义良好的行为。
无论如何,我为您设计了用于普通Androidx Button
的自定义Background
drawable
。
在您的activity_main.xml
您使用属性android:background="@drawable/custom_shape"
定义Button
,您将插入我们稍后定义的drawable
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="10dp">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Collect"
android:background="@drawable/custom_shape"
android:textColor="#ffffff"/>
</RelativeLayout>
这是提到的名为custom_shape.xml
drawable
custom_shape.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ffc9a1"/>
<corners android:radius="10dp"/>
</shape>
</item>
<item android:bottom="5dp">
<shape>
<solid android:color="#f7791e" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
最终结果看起来非常接近您想要的按钮:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.