繁体   English   中英

如何将笔触添加到材料按钮的底部?

[英]How to add stroke to just bottom side of Material button?

我想实现这一目标

我想实现这个按钮。

我试图添加图层列表可绘制背景。

  • 在 MaterialButton 的情况下不起作用。
  • 在普通按钮的情况下它也不起作用,因为我的 AppTheme 是 Material Theme。 所以普通 Button 也表现得像 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM