Android rectangle shape with two different color

前提是你 提交于 2019-12-07 15:48:14

问题


how can I create rectangle shape by using two different colors with shadow? like above image.


回答1:


Please create a drawable file and put the below code in it.

    <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                <shape android:shape="rectangle" >
                    <size android:height="20dp" />
                    <solid android:color="#F66D08" />
                </shape>
            </item>
            <item android:top="50dp">
                <shape android:shape="rectangle" >
                    <gradient android:endColor="#AD1B1D"
                        android:startColor="#E2360A"
                        android:angle="270" />
                </shape>
            </item>
        </layer-list>



回答2:


layer-list can be used to solve this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <size
                android:width="40dp"
                android:height="40dp" />
            <solid android:color="#F86F05" />
        </shape>
    </item>

    <item android:top="10dp">
        <shape android:shape="rectangle">
            <size
                android:width="30dp"
                android:height="30dp" />
            <solid android:color="#B31F19" />
        </shape>
    </item>

</layer-list>

And the screenshot of the same.

If you want gradient instead of solid color, change solid to gradient and set startColor, endColor and angle.




回答3:


Place these code in your activity_main :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:gravity="center"
        android:background="#F66D08"
        android:layout_width="match_parent"
        android:layout_height="20dp">

    </LinearLayout>

    <LinearLayout
        android:background="@drawable/introslidergradiant"
        android:layout_width="match_parent"
        android:layout_height="100dp"></LinearLayout>
</LinearLayout>

And create drawble res file with introslidergradiant.xml name :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:angle="90"
                android:startColor="#A31720"
                android:endColor="#E1350B"
                android:type="linear" />
        </shape>
    </item>
</selector>



回答4:


You have to make a xml file in the drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >
<gradient
android:type="linear"
android:startColor="#FFFF820D"
android:endColor="#FFA32815"
android:angle="90"/>
</shape>

use the following code or you can generate your own gradient here just set the color and select the android option and it will generate the gradient for you .



来源:https://stackoverflow.com/questions/39785961/android-rectangle-shape-with-two-different-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!