给定的按钮颜色未显示,但在 android 工作室的颜色文件中显示颜色

Given button color not showing but showing a color in colors file in android studio

我目前正在 android 工作室从事一个项目。在该应用程序中,所有活动中都有按钮。在 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在颜色文件中。以下颜色在颜色文件中,(原色为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

圆角使用 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" />

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

所以你的按钮表现得像 MaterialButton 所以请将 backgroundTint 设置为“null

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

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

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

则更改它

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

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

Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.


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

下面是对 Button 使用渐变的解决方案:

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