【问题标题】:Given button color not showing but showing a color in colors file in android studio给定按钮颜色未显示但在 android studio 中的颜色文件中显示颜色
【发布时间】:2021-11-11 01:52:57
【问题描述】:

我目前正在 android studio 做一个项目。在该应用程序中,所有活动中都有按钮。在MainActivity(它是应用程序的主菜单)中有3个按钮。我创建了一个名为 roundeddrawable 文件。它用于按钮的背景。以下代码位于rounded 文件中。这会将按钮变成圆形按钮。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#9AFD36"
        android:centerColor="#8CE63E"
        android:endColor="@color/teal_700"
        android:angle="180"
        android:type="linear"/>
    <corners android:radius="10000dp"/>
</shape>

我将这个背景添加到这样的按钮中,

<Button
    android:id="@+id/areabtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Surface Area"
    android:layout_centerInParent="true"
    android:background="@drawable/rounded"
    android:textColor="@color/black"/>

在第 7 行中,我为该文件设置了背景,按钮将在那里显示该渐变,但是当我运行它时,它会变成颜色文件中的 Primary Color。以下颜色在colors文件中,(原色为purple_500

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#11CC6B</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>

这在 values 文件夹的 themes 文件中的代码中。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MathematiciansCalculator" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

我还在其他活动中为后退按钮设置了另一个渐变,但它们也没有显示给定的背景。
Android Studio 版本:- Android Studio Bumblebee | 2021.1.1 金丝雀 11

【问题讨论】:

  • 您是否在代码中的任何位置更改了这些按钮的背景颜色?不仅仅是xml布局
  • 不,我没有那样做。我还创建了一个新项目并尝试过,但发生了同样的事情

标签: java android android-studio button colors


【解决方案1】:

对圆角使用 MaterialButton

 <com.google.android.material.button.MaterialButton
        android:id="@+id/button_share"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:backgroundTint="@color/colorCard"
        android:fontFamily="@font/medium"
        android:insetLeft="0dp"
        android:insetTop="0dp"
        android:insetRight="0dp"
        android:insetBottom="0dp"
        android:text="Share"
        android:textAllCaps="true"
        android:textColor="@color/colorText"
        android:textSize="18sp"
        app:cornerRadius="28dp" />

【讨论】:

    【解决方案2】:

    当您分享您的 应用主题 时,父级是 Theme.MaterialComponents.DayNight.DarkActionBar

    所以您的按钮的行为类似于 MaterialButton,因此请将 backgroundTint 设置为“null

    【讨论】:

      【解决方案3】:

      我在@KGeeks 的帮助下找到了我的问题的答案。正如她所说,它需要app:backgroundTint="@null" 行。

      【讨论】:

        【解决方案4】:

        您好@RedDragon,我认为现有答案的要点是好的但不完整或无法正确解决您的问题:

        app:backgroundTint="@null" 将禁用 MaterialButton 的现有背景颜色,或者如果您输入一个值而不是 null,则更改它。

        提示:你也可以使用app:background="@empty"

        但要提到的重要一点是MaterialButton 不支持渐变,只支持单色。 从文档中了解为什么使用 Tint 而不是 android:background 的简短实地考察:

        不要使用android:background 属性。 MaterialButton 管理自己的背景可绘制对象,并设置新背景 意味着MaterialButton不能再保证新属性 它介绍将正常运行。如果默认背景是 已更改,MaterialButton 无法保证明确定义的行为。


        正如您所见,它与 Android 中的标准小部件非常不同,MaterialDesign 是一个库。

        现在使用带有按钮的渐变的解决方案如下:

        如果您使用 Material Theme 来应用背景渐变,请使用 androidx.appcompat.widget.AppCompatButton,因为如前所述,MaterialButton 尚未提供支持。在那里你可以将drawable 设置为android:background 属性或@style

        【讨论】:

          猜你喜欢
          • 2014-01-23
          • 2020-12-09
          • 2020-09-12
          • 1970-01-01
          • 2021-09-09
          • 1970-01-01
          • 2015-12-06
          • 1970-01-01
          相关资源
          最近更新 更多