【问题标题】:What is alternative of android:background in MaterialComponents?MaterialComponents 中 android:background 的替代方法是什么?
【发布时间】:2019-10-09 10:13:14
【问题描述】:

问题: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 中,此背景不起作用。

我试图改变颜色,形状 n ,但它没有产生效果。

 <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>

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

【问题讨论】:

  • 你能发布一个有这个形状的屏幕吗?
  • @GabrieleMariotti 我无法捕捉到,因为正在发生一些变化。但是我会提供它,我告诉你,它不是圆形的,而且按钮也不是完全居中而是隐藏在左下角'

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


【解决方案1】:

您可以使用 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))

【讨论】:

  • 那么你可以使用代码中的setBackgroundResource 来表示形状,使用xml 中的android:backgroundTint 来表示颜色。希望这会对你有所帮助。
  • 能否添加您的按钮布局?我不知道这里发生了什么。我已经测试过了,它的工作原理。
  • 将按钮 widthheight 更改为 wrap_content
  • @Md.Asaduzzaman 关注setBackgroundResource()。它在 logcat 中调用 setBackgroundDrawable 并带有以下警告:Do not set the background; MaterialButton manages its own background drawable.
  • @GabrieleMariotti,谢谢
【解决方案2】:

由于您使用的是 Material Components 主题,&lt;Button&gt; 在运行时会被 &lt;com.google.android.material.button.MaterialButton&gt; 替换。有一个auto-inflation (check this post)。

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

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

如果您想为MaterialButton 应用自定义背景:

  • 你可以使用 android:backgroundMaterialButton 中,但它至少需要1.2.0-alpha06 版本(检查this answer

  • 按照@Md. Asaduzzaman 的建议使用AppCompatButton

【讨论】:

    【解决方案3】:

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

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

    完成任务的可能方法是使用视图/布局,然后在其上提供形状背景。 例如使用 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>
    

    【讨论】:

      【解决方案4】:

      对于圆形,您可以将形状主题与 Material Components 结合使用。你可能想定义一个ShapeAppearance 喜欢

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

      然后使用 app:shapeAppearance="@style/ShapeAppearance.Round" 将其应用于您的按钮(或按钮样式)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-26
        • 1970-01-01
        • 2022-11-13
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多