MaterialComponents 中 android:background 的替代方法是什么?

What is alternative of android:background in MaterialComponents?

问题:android:background 在 MaterialComponents 中没有生效。

在我的项目中,我使用的是 AppCompat

(<style name="DayTheme" parent="Theme.AppCompat.Light.NoActionBar">) 一切正常。 但是,由于我项目中的一些要求,现在我必须使用 MaterialComponents

( <style name="DayTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> )

因此,有些 UI 看起来很糟糕。

问题:在 AppComapt 中,我使用的是 android:background="@drawable/bg_circle_btn",它工作正常,但在 MaterialComponents 中,这个背景没有生效。

我尝试更改所有颜色和形状,但没有效果。

 <Button
                                android:id="@+id/custom_category_image"
                                android:layout_width="match_parent"
                                android:layout_height="25dp"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:background="@drawable/bg_circle_btn"
                                android:text="A"
                                android:textColor="@color/colorWhite"
                                android:textSize="12dp"
                                android:visibility="gone"
                                app:srcCompat="@drawable/ic_navigate_next_black_24dp" />

(我使用的是按钮,因为我在这个按钮中设置了标题的第一个字母,而不是任何固定的图像,这个按钮实际上在carview里面,所以它也会被循环。)

bg_circle_btn:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="?attr/toolbarcolor" />

    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

请注意,在整个项目中,我需要使用这个背景,所以请不要给出任何其他替代方法。

您可以使用 androidx.appcompat.widget.AppCompatButton 而不是 Button。这将解决您的问题。

除此之外,您可以使用 android:backgroundTint 仅更改颜色。

要为 com.google.android.material.button.MaterialButton 同时更改 (Shape&Color),您必须通过代码执行此操作:

setBackgroundResource(R.drawable.bg_circle_btn) setBackgroundTintList(ColorStateList.valueOf(Color.RED))

由于您使用的是 Material 组件主题,<Button> 在运行时被 <com.google.android.material.button.MaterialButton> 替换。有一个 auto-inflation ().

要更改背景颜色,只需使用 app:backgroundTint 属性。 它使用颜色,而不是可绘制对象。
类似于:

<com.google.android.material.button.MaterialButton
   app:backgroundTint="@color/...."
   ../>

如果要将自定义背景应用于 MaterialButton:

  • 你可以使用 android:background in MaterialButton 但至少需要版本 1.2.0-alpha06(检查

  • 按照

    的建议使用 AppCompatButton

使用 MaterialComponents 主题时不能使用 background 属性。

您只能使用 backgroundTint 属性更改其颜色。

完成任务的可能方法是使用 View/Layout,然后在其上提供形状背景。 例如使用 CardView,您可以按如下方式执行此操作。

您不能在 CardView 上使用 background,但使用一些 CardView 有用的属性可以完成您的任务:

<androidx.cardview.widget.CardView
    android:layout_width="120dp"
    android:layout_height="120dp"
    app:cardBackgroundColor="@color/colorPrimary"
    app:cardCornerRadius="65dp">
    <Widget_You_Want />
</androidx.cardview.widget.CardView>

对于圆形,您可以将形状主题与 Material 组件一起使用。你可能想定义一个 ShapeAppearance

<style name="ShapeAppearance.Round" parent="">
  <item name="cornerFamily">rounded</item>
  <item name="cornerSize">50%</item>
</style>

然后使用 app:shapeAppearance="@style/ShapeAppearance.Round"

将其应用于您的按钮(或按钮样式)