【问题标题】:Android studio Button color not changing - Kotlin [closed]Android工作室按钮颜色没有改变 - Kotlin [关闭]
【发布时间】:2021-03-07 00:01:51
【问题描述】:

这是我的照片, It is the code in which i have written the color code

Here is android blueprint here the color doesn't change

我尝试了很多方法,比如通过样式添加,但都没有成功。 我也尝试过更改背景色调,但也无济于事。 如何更改按钮颜色?
我在上面的图片中分享了我的代码。
帮我换颜色

【问题讨论】:

  • 嘿@Aryan 你的主题是什么。您是否根据要求设置了主题。我很确定按钮的颜色是因为您设置为应用程序的主题
  • @Venky 我有一个主题 xml,但我如何设置主题你能告诉我。我是初学者,所以不太了解,请告诉我
  • @AryanKalekar 使用 android:backgroundTint 代替背景
  • 您可以在 theme.xml 中设置一个按钮主题,然后在按钮之后调用stackoverflow.com/questions/26346727/…

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


【解决方案1】:

由于您使用的是 Theme.MaterialComponents.* 主题,Button 被替换为 MaterialButton,默认情况下,backgroundTint 基于 colorPrimary 在您的应用主题中定义

使用 backgroundTint 属性更改颜色。

您不应该使用android:background 属性来更改颜色,但如果您想使用它来定义自定义背景,您必须避免使用以下方式为按钮着色:

<Button
    app:backgroundTint="@null"/>

同样使用android:background 属性,MaterialButton 不使用自己的MaterialShapeDrawable 作为背景。这意味着形状外观、笔划和圆角等特征将被忽略。

【讨论】:

  • 感谢先生的详细解答。我确定下次我会使用此信息
【解决方案2】:

只需关闭 xml 并重新打开它。您将能够在 xml 视图中找到定义的颜色。与主题无关。它只是 Android Studio xml 更新延迟。

如果它不起作用,则将下面的行添加到您的 Button 标记

app:backgroundTint="your color code"

【讨论】:

  • 它没有帮助,我已经尝试过它以及运行应用程序时。它显示的颜色与我应用的颜色不同
  • 好的。分享您的样式文件。
  • 再次检查我的答案并尝试应用解决方案
  • 如果对您有用,请为答案投票。
【解决方案3】:

将此添加到您的可绘制对象中

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

    android:shape="rectangle">

    <solid android:color="#156667"/>
    <corners android:radius="5dp"/>

</shape>

并在 xml 中将其应用于按钮的背景,您可以在此处更改 &lt;solid android:color="yourColor"/&gt; 的颜色。

【讨论】:

    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 2015-10-29
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2015-10-18
    • 1970-01-01
    相关资源
    最近更新 更多