【发布时间】:2019-11-05 23:52:40
【问题描述】:
我正在尝试创建带有 backgroundTint 渐变的按钮。
我在 drawable 中定义了我的渐变:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerX="50%"
android:centerY="50%"
android:endColor="#308c35"
android:gradientRadius="100dp"
android:startColor="#66BB6A"
android:type="linear" />
<corners android:radius="22dp" />
</shape>
现在我正在尝试将其用作按钮的值:
<com.google.android.material.button.MaterialButton
android:id="@+id/material_unelevated_button"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="160dp"
android:layout_height="58dp"
android:layout_marginBottom="84dp"
android:text="@string/button_start_text"
app:backgroundTint="@drawable/main_gradient"
app:cornerRadius="14dp"
app:iconPadding="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:strokeWidth="0dp" />
很遗憾,它不适用于 backgroundTint。我正在使用材质主题,因此只有更改 backgroundTint 值才能更改按钮的外观。我找不到解决方案,我尝试将 android:background 和所有其他与背景相关的选项设置为渐变,但没有效果。
【问题讨论】:
-
试试下面的代码
-
你做不到。 BackgroundTint 使用颜色不是可绘制的,MaterialButton 也管理自己的背景可绘制