我想在Android上制作一个带有两种背景颜色的按钮,如下图所示:
http://i.stack.imgur.com/ExKXl.png
有没有可能使用可绘制资源来实现?我在http://developer.android.com/guide/topics/resources/drawable-resource.html上寻找解决方案,但它们都不能有两种颜色。
有什么办法吗?
[编辑答案]
解决方案是创建一个<layer-list>
,每个<item>
都有一个<shape>
。代码如下(整个按钮的高度为32dp,因此我为每种颜色使用了一半的高度):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Top color -->
<item android:bottom="16dp">
<shape android:shape="rectangle">
<solid android:color="#FF0000" /> <!-- RED -->
</shape>
</item>
<!-- Bottom color -->
<item android:top="16dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00" /> <!-- GREEN -->
</shape>
</item>
</layer-list>
但我还有一个问题,我试图在每个形状的角上添加圆角。我尝试在第一个形状上放置
android:topLeftRadius
和android:topRightRadius
,在第二个形状上放置android:bottomLeftRadius
和android:bottomRightRadius
,但是它并没有显示出角落!所以解决方案是使用android:radius
(所有8个角都变成了圆角,该死!),并再添加两个项目来克服额外的角落。最终XML如下所示:<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Top color with corner -->
<item android:bottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" /> <!-- It´s obligatory, It didn´t work only with android:topLeftRadius and android:topRightRadius -->
<solid android:color="#FF0000" /> <!-- RED Color-->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="8dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#FF0000" /> <!-- RED Color-->
</shape>
</item>
<!-- Bottom color with corner -->
<item android:top="16dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" /> <!-- It´s obligatory, It didn´t work only with android:bottomLeftRadius and android:bottomRightRadius -->
<solid android:color="#00FF00" /> <!-- GREEN Color -->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="16dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00" /> <!-- GREEN Color -->
</shape>
</item>
</layer-list>
现在它正在运行,谢谢你们!