【问题标题】:How to set a gradient background in a Material Button from Material Components?如何在材质组件的材质按钮中设置渐变背景?
【发布时间】:2019-03-25 06:30:37
【问题描述】:

我一直在尝试从 Material Components for Android 的 Material Button 中设置渐变背景,但它不起作用。 那么,如何在 Material Button 中设置渐变背景呢?

【问题讨论】:

  • 目前无法轻松将背景颜色更改为渐变色。实现这一目标的唯一方法是创建自己的渐变可绘制对象并将其设置为材质按钮。

标签: android gradient android-button material-components-android


【解决方案1】:

从1.2.0-alpha06 版本开始,您可以在MaterialButton 中使用android:background 属性。

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

否则,如果您不能使用 1.2.0-alpha06 或更高版本,则必须使用 AppCompatButton 组件。

关于MaterialButton的一些提示。

  • 目前backgroundTint 仍然是默认的 MaterialButton 样式。
    如果您使用自定义android:background,您必须确保将backgroundTint(app:backgroundTint="@null" 或app:backgroundTint="@empty")置空,以避免自定义背景不着色.
  • 使用自定义android:background 不使用默认MaterialShapeDrawable。未设置某些特征,如笔划、形状外观、波纹(因为它们与 MaterialShapeDrawable 相关)。您必须为他们提供您的自定义背景。

【讨论】:

  • 不得不回来查看 backgroundTint 提示。这是关键!我在我的按钮上放了一个渐变,它的颜色错误。感谢您的指点。
  • 我在使用此方法的按钮上丢失了波纹颜色效果。你知道如何保存涟漪效应吗?
  • @StayCool 使用自定义背景,您将删除同时处理波纹的 ShapeMaterialDrawable。您必须将自定义 RippleDrawable 应用到您的背景。
  • @GabrieleMariotti 我将仅包含形状的 drawable_background.xml 更改为包含形状和波纹项目的 xml :) 效果很好,谢谢!
【解决方案2】:

来自the documentation for MaterialButton:

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

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

因此,不支持通过 MaterialButton 使用渐变背景的方法。

【讨论】:

    【解决方案3】:

    如果您使用 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" />
    

    【讨论】:

      【解决方案4】:

      我找到了这个解决方案。

      <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" />
      

      selector_gradient_example

      <?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>
      

      【讨论】:

      • 嗨,欢迎来到 StackOverflow!答案不应该是简单的 sn-ps 代码。你能详细说明一下这段代码为什么以及如何回答这个问题吗?
      • 此解决方案有效,因为app:backgroundTint 即使使用可绘制设置也无效。将背景设置为可绘制渐变和应用程序背景色调,使渐变在按钮上起作用
      【解决方案5】:

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-21
        • 2018-11-07
        • 2015-03-26
        • 2016-03-10
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        相关资源
        最近更新 更多