按钮背景未更新 - Andorid

发布于 2025-01-14 19:22:21 字数 3950 浏览 1 评论 0 原文

在我的 Android 项目中,我有一个背景为紫色的按钮,但它显示为蓝色。

按钮错误

这是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/background_gradient"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="50dp"
        android:background="@color/blue">

        <Button
            android:id="@+id/Button_StartGame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/purple"
            android:layout_margin="10dp"
            android:text="@string/start_game" />

    </RelativeLayout>
    
</LinearLayout>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="lawnGreen">#7CFC00</color>
    <color name="neonGreen">#39FF14</color>
    <color name="cyan">#00FFFF</color>
    <color name="blue">#0000FF</color>
    <color name="yellow">#FFFF00</color>
    <color name="purple">#800080</color>
    <color name="pink">#EE82EE</color>
    <color name="black">#000000</color>
    <color name="white">#FFFFFF</color>
</resources>

theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryVariant">@color/purple</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue</item>
        <item name="colorSecondaryVariant">@color/purple</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>

theme.xml (night)

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryVariant">@color/purple</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue</item>
        <item name="colorSecondaryVariant">@color/purple</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>

我没有提供 MainActivity.java 因为我还没有自定义它,这只是一个新项目。

我该如何解决这个问题?

In my Android project, I have a button with a background of purple, but it is showing blue.

Button Error

This is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/background_gradient"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="50dp"
        android:background="@color/blue">

        <Button
            android:id="@+id/Button_StartGame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/purple"
            android:layout_margin="10dp"
            android:text="@string/start_game" />

    </RelativeLayout>
    
</LinearLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="lawnGreen">#7CFC00</color>
    <color name="neonGreen">#39FF14</color>
    <color name="cyan">#00FFFF</color>
    <color name="blue">#0000FF</color>
    <color name="yellow">#FFFF00</color>
    <color name="purple">#800080</color>
    <color name="pink">#EE82EE</color>
    <color name="black">#000000</color>
    <color name="white">#FFFFFF</color>
</resources>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryVariant">@color/purple</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue</item>
        <item name="colorSecondaryVariant">@color/purple</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>

themes.xml (night)

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MultiSmart" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryVariant">@color/purple</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue</item>
        <item name="colorSecondaryVariant">@color/purple</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>

I didn't provide MainActivity.java because I didn't customize it yet, it's just a new project.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2025-01-21 19:22:21

原因:

android:background 在按钮中没有显示正确的颜色,因为使用材质组件主题时;应用了默认色调(即它不是像之前的 AppCompat 主题那样的 null ),并且它会影响按钮颜色。

解决方案:

您可以通过对按钮应用以下几种方法之一来解决此问题:

  • 使用 android:backgroundTint 而不是 android:background
  • 使用 将色调设置为 null app:backgroundTint="@null",通常使用android:background

Cause:

The android:background doesn't show up the right color in the button because when using a material components theme; there is a default tint color applied (i.e. it's not null like in the previous AppCompat themes), and it affects the button color.

Solution:

You can solve this by applying one of a couple of ways to the button:

  • Use android:backgroundTint instead of android:background
  • Set the tint to null with app:backgroundTint="@null", and normally use android:background
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文