25

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

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

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

我查看了文档,但找不到有关此更改的任何提及。

4

8 回答 8

19

在材质组件库中,MaterialButton有一个默认样式,其中insetBottominsetTop值为6dp

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

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

在此处输入图像描述

如果要更改背景颜色,可以使用该app:backgroundTint属性,也可以从默认样式覆盖某些主题属性,然后可以使用新materialThemeOverlay属性。

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

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

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

最后从版本开始,1.2.0-alpha06您可以android:background使用MaterialButton.

<MaterialButton
    app:backgroundTint="@null"
    android:background="@drawable/button_drawable"
    ... />
于 2019-08-17T18:18:38.127 回答
18

MaterialButton该类的文档说:

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

但是,GitHub 自述文件说:

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

这仅提及左/右插图,但自述文件的属性部分显示支持所有四个插图:

在此处输入图像描述

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

android:insetTop="0dp"
android:insetBottom="0dp"
于 2018-10-05T20:55:09.073 回答
8

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

您也可以替换<Button<android.widget.Button.

于 2020-05-12T11:17:01.460 回答
3
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" />
于 2021-05-01T16:56:53.930 回答
2

当我在 Button 中使用 state drawable 时,我遇到了同样的问题,但它不会改变按钮的背景。找了半天,找到了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" />

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

于 2021-01-29T09:36:31.067 回答
1

如果您想保留您的

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-09-16T17:14:52.480 回答
0

只需使用 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"/>
于 2021-05-09T15:52:03.543 回答
0

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

另外,我在迁移到 MDC 时从谷歌教程中找到了这个片段。

用此代码替换

于 2021-07-19T15:51:04.843 回答