我一直在尝试在 Android 的 Material Components 中为 Material 按钮设置渐变背景,但是它没有生效。 那么,如何在 Material 按钮中设置渐变背景呢?
我一直在尝试在 Android 的 Material Components 中为 Material 按钮设置渐变背景,但是它没有生效。 那么,如何在 Material 按钮中设置渐变背景呢?
从版本1.2.0-alpha06
开始,您可以在MaterialButton
中使用android:background
属性。
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/button_gradient"
... />
不要使用
android:background
属性。 MaterialButton管理自己的背景可绘制对象,并设置新背景意味着MaterialButton
无法再保证其引入的新属性将正常工作。如果更改默认背景,则MaterialButton
无法保证定义良好的行为。
修改按钮背景的唯一支持方法是将颜色值设置为app:backgroundTint
属性。不幸的是,这里也不支持渐变;您只能使用简单的颜色值或ColorStateList
。
因此,没有受支持的方法可以在MaterialButton中使用渐变背景。
如果您使用Material Theme并希望应用背景渐变,请使用androidx.appcompat.widget.AppCompatButton
,因为Material Button目前还不支持此功能。
对于背景渐变,请在drawable
文件夹中创建3个新资源:
selector_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="@drawable/btn_gradient_inactive" android:state_enabled="false" />
<item android:drawable="@drawable/btn_gradient_active" />
</selector>
btn_gradient_inactive.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#01579B"
android:endColor="#1A237E"
android:angle="0" />
<corners android:radius="5dp"/> </shape>
btn_gradient_active.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#2196F3"
android:endColor="#3F51B5"
android:angle="0" />
<corners android:radius="5dp"/>
</shape>
在你的 styles.xml
文件中:
<style name="BtnStyle" parent="Widget.AppCompat.Button.Borderless">
<item name="android:layout_marginStart">35dp</item>
<item name="android:layout_marginEnd">35dp</item>
<item name="android:background">@drawable/selector_btn_gradient</item>
<item name="android:textColor">@color/selector_text_color</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">14sp</item>
</style>
在你的layout.xml
文件中:
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/my_btn_id"
style="@style/BtnStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_gradient_example"
app:backgroundTint="@null" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/banana_yellow">
<item android:state_enabled="true">
<ripple android:color="@color/banana_yellow">
<item>
<shape android:shape="rectangle">
<gradient android:angle="135" android:endColor="@color/banana_yellow" android:startColor="@color/main_yellow" />
</shape>
</item>
</ripple>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<gradient android:angle="315" android:endColor="#ede0be" android:startColor="#e0bf7e" />
</shape>
</item>
</selector>
app:backgroundTint
,它也没有任何效果。将背景设置为drawable渐变和应用程序背景色调,可以使渐变在按钮上起作用。 - 89n3ur0n在XML中删除backgroundTint属性对我有效。
btn_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#469FED"
android:angle="90"
android:endColor="#2AC88D"/>
<corners android:radius="4dp"/>
</shape>
使用此代码替代按钮:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:background="@drawable/btn_gradient">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="40dp"
android:background="?attr/selectableItemBackground"
android:padding="5dp"
android:text="send" />
</RelativeLayout>