【问题标题】:Material Button background gradient材质按钮背景渐变
【发布时间】:2018-07-27 16:11:33
【问题描述】:

有没有办法我可以从谷歌材料库中为 MaterialButton 设置渐变颜色。 app:backgroundTint 只设置颜色不设置渐变色

【问题讨论】:

    标签: android android-button material-components


    【解决方案1】:

    MaterialButton 忽略 android:background 直到发布 1.2.0-alpha06

    在此版本中,您可以使用以下内容:

    <Button
        android:background="@drawable/bg_button_gradient"
        app:backgroundTint="@null"
        ... />
    

    如果您在早期版本的库中需要此功能,您可以使用AppCompatButton。比如:

    <androidx.appcompat.widget.AppCompatButton
        android:background="@drawable/bg_button_gradient"
    

    【讨论】:

      【解决方案2】:

      对此不确定,但根据developer.android.comandroid:background都加不了,看来materialButtondrawable是不行的。

      【讨论】:

        【解决方案3】:

        使用默认方法无法做到这一点。更好地使用 gradient drawable

        创建一个新的xml文件并将其放入drawable中,然后将其作为背景添加到按钮中

        gradient.xml

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
           <gradient
              android:startColor="#000000"
              android:centerColor="#ffffff"
              android:endColor="#cfcfcf"
              android:angle="270" />
        </shape>
        

        layout.xml

         <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/gradient"
            android:text="YourText" >
        

        【讨论】:

        • 我知道该怎么做。我想知道的是它可以通过谷歌的材料组件库在 MaterialButton 上完成
        • 没有这样做的默认方法。
        【解决方案4】:

        创建可绘制文件,例如

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:angle="135"
            android:centerColor="#6200ee"
            android:endColor="#6200ee"
            android:startColor="#3700b3"
            android:type="linear" />
         </shape>
        

        并将此文件设置为按钮的背景

        【讨论】:

        • 是否可以在 MaterialButton 上实现
        猜你喜欢
        • 2019-02-21
        • 2019-03-25
        • 2012-08-26
        • 2015-03-26
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 2014-12-28
        相关资源
        最近更新 更多