You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android按钮显示颜色与设置不符问题求助

Hey there! Let's figure out why your button is showing purple instead of the green you set in your Shape drawable, or if you actually want it to stay purple, how to adjust your code properly.

First, let's recap your setup:

  • Your button's XML references @drawable/btn_login as its background
  • That Shape drawable defines a solid green color (#71D775) with rounded corners

If you want the button to show the green you configured

The most common issue here is Android's default theme overriding your custom background. Here are a few straightforward fixes:

  1. Disable theme-based background tinting
    Add android:backgroundTint="@null" to your button's XML attributes. This stops the system theme from applying its own color over your Shape:
<Button
    android:id="@+id/btnLogin"
    android:layout_width="128dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="141dp"
    android:layout_marginEnd="142dp"
    android:layout_marginBottom="48dp"
    android:background="@drawable/btn_login"
    android:backgroundTint="@null" <!-- Add this line -->
    android:fontFamily="@font/poppins"
    android:text="Login"
    android:textAllCaps="false"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/tvCreateAccount"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
  1. Switch to AppCompatButton
    If you're using the default Button widget, switching to androidx.appcompat.widget.AppCompatButton avoids common theme conflicts with custom backgrounds:
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btnLogin"
    android:layout_width="128dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="141dp"
    android:layout_marginEnd="142dp"
    android:layout_marginBottom="48dp"
    android:background="@drawable/btn_login"
    android:fontFamily="@font/poppins"
    android:text="Login"
    android:textAllCaps="false"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/tvCreateAccount"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
  1. Check for dynamic background changes
    Double-check your Activity/Fragment code to make sure there's no logic overriding the button's background (like btnLogin.setBackgroundColor(...) or btnLogin.setBackgroundResource(...)). These will overwrite your XML settings.

If you actually want the button to stay purple

Just update the solid color in your btn_login.xml Shape drawable to a purple hex code (I'll use #9C27B0 as an example):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#9C27B0" /> <!-- Update this color -->
    <corners android:radius="66dp" />
</shape>

After making any changes, be sure to clean and rebuild your project to ensure the resources update properly!

内容的提问来源于stack exchange,提问作者adirk1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:34:07