简体   繁体   中英

Android GradientDrawable Looks Different on Layout Editor Preview

Why are the gradients looking different in Android Studio preview and in an actual Android device? Here are the screenshots:
在此处输入图片说明
In Android Studio

在此处输入图片说明
In my Android phone

This is the drawable/btn_background.xml file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#eee"
                android:endColor="#aaa"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#aaa"
                android:endColor="#eee"/>
        </shape>
    </item>
</selector>

This is the XML code for the two buttons:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="display"
            android:text="@string/btn_txt"
            android:textAllCaps="false"
            android:background="@drawable/btn_background"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/btn_background"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_fullscreen_black" />

Is it a bug or something?

I have got a solution to my problem. I have just added the android:angle property in the gradient drawable:

<item android:state_pressed="false">
    <shape android:shape="rectangle">
        <gradient
            android:startColor="#eee"
            android:endColor="#aaa"
            android:angle="0"/>
    </shape>
</item>

Without the android:angle property, Android Studio and Android phones were not agreeing with each other. So, defining the angle solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM