【问题标题】:Can't use android:background with button from the new material components无法使用 android:background 和新材质组件中的按钮
【发布时间】:2019-03-11 09:36:18
【问题描述】:

我在 android x 中使用新的材质组件com.google.android.material:material,但我无法为按钮设置自定义背景。

我知道我可以使用app:backgroundTint来改变颜色

但默认背景有一些我想摆脱的填充,以及使用android:background 设置我自己的背景的旧方法,但这不再有效。

我查看了docs,但找不到任何关于此更改的信息。

【问题讨论】:

  • 您可以使用 ImageButton 代替 Material 组件中的 Button。 android:background 正在 ImageButton 上工作。

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


【解决方案1】:

The documentation for the MaterialButton class 说:

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

但是,the GitHub readme 说:

注意:MaterialButton 在视觉上与Button 和AppCompatButton 不同。主要区别之一是AppCompatButton 在左右两侧有一个4dp,而MaterialButton 没有。

这里只提到了左/右插图,但自述文件的the Attributes section 表明支持所有四个插图:

因此,您可以将这些属性添加到您的 <MaterialButton> 标记中:

android:insetTop="0dp"
android:insetBottom="0dp"

【讨论】:

  • 不,我需要担心它,因为它仍然显示在按钮和顶部。
  • 您是否尝试在您的<MaterialButton> 标签上设置android:insetTop="0dp"(和insetBottom)?
  • 那么我们如何在materialButton中添加渐变呢?
  • 对我不起作用,按钮仍然是深黑色背景
  • 这不是解决方案。请参阅stackoverflow.com/a/52751122/2914140 或以下解决方案。
【解决方案2】:

在材质组件库中,MaterialButton 的默认样式为insetBottom 和insetTop,其值为6dp。

您可以使用以下方法更改它:

  <com.google.android.material.button.MaterialButton
      android:insetTop="0dp"
      android:insetBottom="0dp"
      ../>

如果你想改变背景颜色你可以使用app:backgroundTint属性或者你可以覆盖一些默认样式的主题属性然后你可以使用新的materialThemeOverlay strong> 属性。

在您的情况下,您可以执行以下操作:

<style name="MtButtonStyle"
 parent="Widget.MaterialComponents.Button">
   <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>

<style name="GreenButtonThemeOverlay">
  <item name="colorPrimary">@color/green</item>
</style>

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

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

【讨论】:

  • 请注意版本 1.1.0 处于 alpha 状态现在 mvnrepository.com/artifact/com.google.android.material/material
  • @eugeneeek 是的,它仍处于 alpha 阶段,本周将处于 beta 阶段。
  • @GabrieleMariotti,我经常看到您使用支持库的 alpha 和 beta 版本。您如何知道他们何时获得了新功能?你在谷歌工作吗? :)
【解决方案3】:

查看https://medium.com/@velmm/material-button-in-android-e4391a243b17,我发现app:backgroundTint(和app:backgroundTintMode)有效。它会更改颜色,但不会更改可绘制选择器。

Also 您可以将&lt;Button 替换为&lt;android.widget.Button。

【讨论】:

  • 你保护了我的生命。感谢您的解决方案。
【解决方案4】:

当我在按钮中使用可绘制状态时遇到了同样的问题,但它不会改变按钮的背景。找了半天,找到了2个解决方法:
第一个解决方案是在 values/themes.xml 文件中将应用程序主题从 MaterialComponents 更改为 AppCompat。然后 state drawable 会很好地工作。

到

    <style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">

如果您仍想使用 MaterialComponents 主题,那么您可以尝试第二种解决方案。
使用 android.widget.Button 而不是 Button 或 com.google.android.material.button.MaterialButton

    <android.widget.Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/state_test"
        android:text="Button" />

我在Here找到的第二个解决方案

【讨论】:

    【解决方案5】:
    If you want to use gradient drawable file as MaterialButton background you can follow these simple steps.
    
    set Your MaterialButton like this below
    
    <com.google.android.material.button.MaterialButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            app:backgroundTint="@null"
            android:background="@drawable/group_47"
            app:layout_constraintEnd_toEndOf="@+id/input_password"
            app:layout_constraintStart_toStartOf="@+id/input_password"
            app:layout_constraintTop_toBottomOf="@+id/input_password" />
    

    【讨论】:

    • 不起作用,您是否添加了其他东西使其起作用?仅将背景和色调设置为 null 不起作用
    • @ZeusAlmighty 请告诉我你的要求,以及你是如何尝试的
    • 我通过设置background=@drawable/custom_bg 和backgroundTint="@null" 进行了尝试。我想使用背景可绘制的颜色作为材质按钮的背景,但它使用原色作为背景。
    • @ZeusAlmighty 你能分享你的材质按钮和style.xml文件吗
    • 如何发送?
    【解决方案6】:

    只需使用 android:backgroundTint

    <style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
        <item name="android:backgroundTint">@color/pink</item>
    </style>
    
    
    <com.google.android.material.button.MaterialButton
        android:id="@+id/followButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/channel_header_item_margin"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/titeChannelTV"
        style="@style/MyButtonStyle"/>
    

    【讨论】:

      【解决方案7】:

      谷歌设计团队决定将android:background="@drawable/"从材料库的初始版本中排除的主要原因是为了让开发人员能够以更快的速度为应用程序构建一致且具有专业外观的设计。这是因为像我这样的大多数开发人员在做出与应用的设计和颜色相关的决策时都很糟糕。

      另外,我在迁移到 MDC 时从 google tutorial 发现了这个 sn-p。

      【讨论】:

        【解决方案8】:

        如果你想保留你的

        android:background="@drawable/button_background"
        

        并且有MaterialButton尊重它,那么你必须设置

        app:backgroundTint="@null"
        app:backgroundTintMode="add" // it doesn't matter the value, but it must be set
        

        请注意,您也可以改用 app:background,尽管我注意到有足够多的重大更改,因此我仍然更喜欢上述方法。

        【讨论】:

          猜你喜欢
          • 2021-04-13
          • 2022-01-10
          • 2017-07-01
          • 2019-03-25
          • 1970-01-01
          • 1970-01-01
          • 2021-05-11
          • 1970-01-01
          • 2020-08-02
          相关资源
          最近更新 更多