MaterialComponents中android:background的替代方法是什么? [英] What is alternative of android:background in MaterialComponents?

查看:66
本文介绍了MaterialComponents中android:background的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:android:background在MaterialComponents中不起作用.

Question: android:background is not giving effect in MaterialComponents.

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

In my project, I was using AppCompat

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

( <style name="DayTheme" parent="Theme.AppCompat.Light.NoActionBar">) and everything was working fine. but, because of some requirement in my project, now, I've to use MaterialComponents

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

因此,某些UI看起来很糟糕.

And because of that some UI looks bad.

问题:在AppComapt中,我使用的android:background="@drawable/bg_circle_btn"正常,但是在MaterialComponents中,这种背景没有效果.

Problem: In AppComapt, I was using android:background="@drawable/bg_circle_btn" which was working fine, but in MaterialComponents, this background is not giving effect.

我试图改变颜色,将形状全部改变,但没有效果.

I tried to change color, shape n all but it's not giving effect.

 <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内,所以也将是cirlce.)

(I'm using button, because, instead of any fixed image, I'm setting the first letter of title in this button and this button is actually inside carview, so it'll be cirlce also.)

bg_c​​ircle_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>

请注意,在整个项目中,我需要使用此背景,因此请不要提供任何其他替代方法.

Please note that, in whole project, I need to use this background, so please do not give any other alternative ways.

推荐答案

您可以使用 androidx.appcompat.widget.AppCompatButton 代替Button.这样可以解决您的问题.

You could use androidx.appcompat.widget.AppCompatButton instead of Button. This will solve your problem.

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

Besides this you can use android:backgroundTint to change the color only.

要同时更改 com.google.android.material.button.MaterialButton 的两种(形状和颜色),必须从代码中执行以下操作:

To change both (Shape&Color) for com.google.android.material.button.MaterialButton, you have to do this from you code:

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

这篇关于MaterialComponents中android:background的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆