我想制作一个填充超过 3 种颜色的按钮,比如 7 色彩虹,从左 = 红色到右 = 紫色。
但我发现下面的代码只能满足 3 种颜色。
问题:
有没有办法生成彩虹渐变?我手上有一个 Rainbow.png,可以用吗?谢谢!!
当前代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
...
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<gradient
android:startColor="@color/red"
android:centerColor="@color/green"
android:endColor="@color/purple"
android:angle="0" />
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
</selector>
回答:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<solid android:color="@color/grey"/>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:drawable="@drawable/rainbow" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
</selector>