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_loginas 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:
- Disable theme-based background tinting
Addandroid: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" />
- Switch to AppCompatButton
If you're using the defaultButtonwidget, switching toandroidx.appcompat.widget.AppCompatButtonavoids 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" />
- Check for dynamic background changes
Double-check your Activity/Fragment code to make sure there's no logic overriding the button's background (likebtnLogin.setBackgroundColor(...)orbtnLogin.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
相关产品推荐
相关产品推荐

