【发布时间】:2018-07-27 16:11:33
【问题描述】:
有没有办法我可以从谷歌材料库中为 MaterialButton 设置渐变颜色。 app:backgroundTint 只设置颜色不设置渐变色
【问题讨论】:
标签: android android-button material-components
有没有办法我可以从谷歌材料库中为 MaterialButton 设置渐变颜色。 app:backgroundTint 只设置颜色不设置渐变色
【问题讨论】:
标签: android android-button material-components
MaterialButton 忽略 android:background 直到发布 1.2.0-alpha06。
在此版本中,您可以使用以下内容:
<Button
android:background="@drawable/bg_button_gradient"
app:backgroundTint="@null"
... />
如果您在早期版本的库中需要此功能,您可以使用AppCompatButton。比如:
<androidx.appcompat.widget.AppCompatButton
android:background="@drawable/bg_button_gradient"
【讨论】:
对此不确定,但根据developer.android.com
连android:background都加不了,看来materialButton加drawable是不行的。
【讨论】:
使用默认方法无法做到这一点。更好地使用 gradient drawable。
创建一个新的xml文件并将其放入drawable中,然后将其作为背景添加到按钮中
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>
并将此文件设置为按钮的背景
【讨论】: