简体   繁体   English

如何在自定义形状中填充两种不同的颜色?

[英]How to fill two different colors in Custom Shape?

I have created the custom shape in android project. 我在android项目中创建了自定义形状。 here is code, 这是代码,

curvedShape.xml:- curvedShape.xml: -

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="4dp" android:color="#ffffff" />
    <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <gradient android:centerColor="#ffffff" android:startColor="#32CD32" android:endColor="#ffffff"/> 
</shape>

main.xml:- main.xml中: -

<View   
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_marginLeft = "10dp"
            android:layout_marginRight = "10dp"
            android:layout_centerHorizontal="true" 
            android:layout_marginTop="20dp"
            android:background="@drawable/curvedshape"/>

In shape,gradient attribute gave start color as green, that filled green color vertically. 在形状上,渐变属性将起始颜色设为绿色,垂直填充绿色。 but i want to fill the color like the below image, fill color in horizontal in half shape. 但我想填充下面图像的颜色,填充水平半色的颜色。 How to do that? 怎么做?

在此输入图像描述

Use layer-list and draw two shapes in XML: 使用layer-list并在XML中绘制两个形状:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >   
    < item>
            < shape
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle" >

           <stroke android:width="4dp" android:color="#ffffff" />

        </shape>
    </item>      
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >

            <stroke android:width="4dp" android:color="#7CFC00" />
        </shape>
    </item> 
</layer-list>

Edits : you have just use this gradient attribute in your xml 编辑:您只需在xml中使用此渐变属性

< gradient android:angle="-90" android:centerX="0.5" android:centerY="0.5" android:centerColor="#ffffff" android:startColor="#32CD32" android:endColor="#ffffff"
 /> 

I have use this and this show similar shape required by you: 我使用了这个,这显示了你需要的类似形状:

 < shape
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle" >
 < gradient android:angle="-90" android:centerX="0.5" android:centerY="0.5" android:centerColor="#ffffff" android:startColor="#32CD32" android:endColor="#ffffff"
 /> 
 < /shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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