Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

Gradient color not applied to button in android studio

I have created a gradient_file.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp" />

    <gradient
        android:startColor="@color/white"
        android:endColor="@color/black"
        android:angle="45"
        android:type="linear" />
</shape>

I have applied it to button in my layout

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:background="@drawable/gradient_file"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.243" />

This is the result i am getting:

enter image description here

The color should be different. i have rebuild the project but the color does not change in preview nor in emulator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

it is getting overwritten by the colors in themes.xml file. Go to your themes.xml file (in res/values/themes) and replace

 <item name="colorPrimary">@color/purple_500</item>

with

<item name="colorPrimary">@null</item>

to see your gradient work.

EDIT: I'm not sure why this is... could be a bug in newer android studio versions? But I was able to get your button gradient working with this solution


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...