我正在使用com.google.android.material:material
带有 android x 的新材质组件,但我无法为按钮设置自定义背景。
我知道我可以用它app:backgroundTint
来改变颜色
但是默认背景有一些我想摆脱的填充,以及android:background
用于设置我自己的背景的旧方法,但这不再有效。
我查看了文档,但找不到有关此更改的任何提及。
我正在使用com.google.android.material:material
带有 android x 的新材质组件,但我无法为按钮设置自定义背景。
我知道我可以用它app:backgroundTint
来改变颜色
但是默认背景有一些我想摆脱的填充,以及android:background
用于设置我自己的背景的旧方法,但这不再有效。
我查看了文档,但找不到有关此更改的任何提及。
在材质组件库中,MaterialButton
有一个默认样式,其中insetBottom
和insetTop
值为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”>@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"
... />
不要使用该
android:background
属性。MaterialButton
管理自己的背景drawable,设置新的背景意味着MaterialButton
不能再保证它引入的新属性会正常工作。如果更改默认背景,MaterialButton
则无法保证良好定义的行为。
但是,GitHub 自述文件说:
注意:
MaterialButton
在视觉上与Button
和不同AppCompatButton
。主要区别之一是在左侧和右侧AppCompatButton
有一个插图,而没有。4dp
MaterialButton
这仅提及左/右插图,但自述文件的属性部分显示支持所有四个插图:
因此,您可以将这些属性添加到您的<MaterialButton>
标签中:
android:insetTop="0dp"
android:insetBottom="0dp"
查看https://medium.com/@velmm/material-button-in-android-e4391a243b17我发现app:backgroundTint
(和app:backgroundTintMode
)有效。它会改变颜色,但不会改变可绘制的选择器。
您也可以替换<Button
为<android.widget.Button
.
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" />
当我在 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" />
如果您想保留您的
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代替,尽管我已经注意到足够多的重大更改,因此我仍然更喜欢上面的方法。
只需使用 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"/>
android:background="@drawable/"
谷歌设计团队决定从材料库的初始版本中排除该功能的主要原因是为了让开发人员能够以更快的速度为应用程序构建一致且具有专业外观的设计。这是因为大多数像我这样的开发人员在做出与应用程序的设计和颜色相关的决策时都很糟糕。
另外,我在迁移到 MDC 时从谷歌教程中找到了这个片段。