我能否通过谷歌材料库为MaterialButton设置渐变颜色?app:backgroundTint只能设置颜色而无法设置渐变颜色。
我能否通过谷歌材料库为MaterialButton设置渐变颜色?app:backgroundTint只能设置颜色而无法设置渐变颜色。
MaterialButton
在发布 1.2.0-alpha06
前会忽略 android:background
属性。
从这个版本开始,您可以使用以下代码:
<Button
android:background="@drawable/bg_button_gradient"
app:backgroundTint="@null"
... />
AppCompatButton
。类似这样:<androidx.appcompat.widget.AppCompatButton
android:background="@drawable/bg_button_gradient"
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#000000"
android:centerColor="#ffffff"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
layout.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gradient"
android:text="YourText" >
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#6200ee"
android:endColor="#6200ee"
android:startColor="#3700b3"
android:type="linear" />
</shape>
并将此文件设置为按钮的背景