【问题标题】:How to remove tint on icon in Material Button?如何删除材质按钮中图标上的色调?
【发布时间】:2019-03-23 14:33:48
【问题描述】:

我想要一个带有白色背景和彩色图标的“使用 Google 登录”按钮,但是当我使用这个 Google 图标时,它总是会被着色。

  <android.support.design.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sign in with Google"
    app:icon="@drawable/ic_google_colored"/>

如何使图标不着色?

【问题讨论】:

    标签: android material-design material-components material-components-android


    【解决方案1】:

    我已经找到解决办法了,我只是换了iconTintMode和iconTint颜色

      <android.support.design.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign in with Google"
        app:icon="@drawable/ic_google_colored"
        app:iconTint="@android:color/transparent"
        app:iconTintMode="add"/>
    

    我愿意接受更好的解决方案

    【讨论】:

      【解决方案2】:

      使用这个并退出色调图标以显示真实颜色

        app:icon="@drawable/ic_google"
        app:iconTint="@null"
      

      例子

      <Button
        android:id="@+id/idGoogle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:icon="@drawable/ic_google"
        app:iconTint="@null"/>
      

      icon with real color shows black background

      【讨论】:

      • 接受的答案不适用于 OutlinedButton。此解决方案也适用于 OutlinedButton ?
      【解决方案3】:

      您还可以使用白色作为 iconTint,并使用乘法作为 iconTintMode 来获得彩色图标。

       app:iconTint="@color/white"
       app:iconTintMode="multiply"
      

      【讨论】:

        【解决方案4】:

        这些答案都不适合我。但经过大量试验后,我发现这可以解决问题。

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitCenter"
            android:background="#0000"
            android:foreground="@android:drawable/ic_menu_camera"
            />
        

        似乎密钥是android:background="#0000"。不知道为什么 @null 不起作用,但这就是最终让那个愚蠢的颜色消失的原因。

        【讨论】:

          【解决方案5】:
          button.iconTint = ColorStateList.valueOf(Color.TRANSPARENT)
          

          【讨论】:

            猜你喜欢
            • 2020-02-06
            • 2019-04-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-13
            • 1970-01-01
            • 2018-11-20
            相关资源
            最近更新 更多