gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:10 27 4
gpt4 key购买 nike

我已经在 android 项目中创建了自定义形状。这是代码,

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>

ma​​in.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"/>

在shape中,gradient属性给起始颜色为绿色,垂直填充绿色。但我想像下图一样填充颜色,水平填充颜色的一半。如何做到这一点?

enter image description here

最佳答案

使用 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>

编辑:你刚刚在你的 xml 中使用了这个渐变属性

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

我用过这个和你要求的形状相似:

 < 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>

关于java - 如何在自定义形状中填充两种不同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com