Android 如何从材质组件在材质按钮中设置渐变背景?

Android 如何从材质组件在材质按钮中设置渐变背景?,android,gradient,android-button,material-components-android,Android,Gradient,Android Button,Material Components Android,我一直在尝试在Android材质组件的材质按钮中设置渐变背景,但它不起作用。 那么,如何在材质按钮中设置渐变背景?来自: 不要使用android:background属性。MaterialButton管理自己的后台绘图,设置新的后台意味着MaterialButton不能再保证它引入的新属性正常工作。如果更改了默认背景,MaterialButton无法保证定义良好的行为 修改按钮背景的唯一受支持的方法是将颜色值设置为app:backgroundTint属性。不幸的是,这里也不支持渐变;您只能使用简

我一直在尝试在Android材质组件的材质按钮中设置渐变背景,但它不起作用。 那么,如何在材质按钮中设置渐变背景?

来自:

不要使用
android:background
属性。MaterialButton管理自己的后台绘图,设置新的后台意味着
MaterialButton
不能再保证它引入的新属性正常工作。如果更改了默认背景,
MaterialButton
无法保证定义良好的行为

修改按钮背景的唯一受支持的方法是将颜色值设置为
app:backgroundTint
属性。不幸的是,这里也不支持渐变;您只能使用简单的颜色值或
颜色状态列表


因此,没有支持的方法可以将渐变背景与MaterialButton一起使用。

如果您使用Material主题来应用背景渐变,请使用
androidx.appcompat.widget.AppCompatButton
,因为Material Button尚未提供对其的支持

对于背景渐变,在
drawable
文件夹中创建3个新资源:

选择器_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>
<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>
<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>
    <?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>
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" />

btn\u gradient.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>
<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>
<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>
    <?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>

从版本开始,您可以使用
MaterialButton
中的
android:background
属性

<MaterialButton
    app:backgroundTint="@null"
    android:background="@drawable/button_gradient"
    ... />

否则,如果无法使用1.2.0-alpha06或更高版本,则必须使用AppCompatButton组件

下面是一些关于
材质按钮的提示

  • 目前,
    backgroundTint
    仍然是默认的MaterialButton样式。
    如果您使用的是自定义的
    android:background
    ,则必须确保将
    backgroundTint
    (要么
    app:backgroundTint=“@null”
    要么
    app:backgroundTint=“@empty”
    ),以避免自定义背景不着色
  • 使用自定义的
    android:background
    不使用默认的
    materialshapedravable
    。未设置笔划、形状外观、纹波等某些功能(因为它们与
    材质形状可拖动
    相关)。您必须为他们提供您的自定义背景
    • 我找到了这个解决方案

      <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>
      
      
      
      当前无法轻松将背景颜色更改为渐变色。你唯一能做到这一点的方法是创建你自己的渐变绘图,并将其设置为“材质”按钮。你必须回来查看背景色提示。这是钥匙!我在我的按钮上加了一个渐变,它的颜色不对。谢谢你的指针。我在使用这种方法的按钮上失去了涟漪色彩效果。你知道如何保存涟漪效果吗?@StayCool使用自定义背景,你正在删除ShapeMaterialDrawable,它同时处理涟漪。您必须对您的背景应用自定义的RippleDrawable。@GabrieleMariotti我将只包含形状的drawable_background.xml更改为包含形状和ripple项的xml:)效果很好,谢谢!嗨,欢迎来到StackOverflow!答案不应该是简单的代码片段。您能否详细说明此代码回答问题的原因和方式?此解决方案有效,因为
      app:backgroundTint
      即使设置为drawable,也没有任何效果。将背景设置为drawable gradient和app background tint,使渐变在按钮上工作