如何在“材质组件”的“材质按钮”中设置渐变背景?

时间:2018-10-20 17:00:15

标签: android gradient material-components-android

我一直在尝试从Android的Material Components中的Material Button中设置渐变背景,但是它不起作用。 因此,是否可以在“材质”按钮中设置渐变背景?如果是这样,如何实现?

5 个答案:

答案 0 :(得分:13)

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

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

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

关于 MaterialButton 的一些提示。

  • 当前backgroundTint仍然是默认的MaterialButton样式。
    如果您使用的是自定义android:background,则必须确保清空 backgroundTintapp:backgroundTint="@null"app:backgroundTint="@empty"),以避免这种情况自定义背景不会被着色。
  • 使用自定义android:background,不使用默认MaterialShapeDrawable。某些功能(例如笔触,形状外观,波纹)未设置(因为它们与MaterialShapeDrawable相关)。您必须为他们提供自定义背景。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

我找到了解决方案。

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

答案 3 :(得分:1)

来自the documentation for MaterialButton

  

请勿使用android:background属性。 MaterialButton管理其自己的背景可绘制对象,并且设置新的背景意味着MaterialButton不再能够保证其引入的新属性将正常运行。如果更改了默认背景,MaterialButton将无法保证行为明确。

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

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

答案 4 :(得分:-2)

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>