安卓按钮有两个背景颜色 [英] Android: button with two background colors
问题描述
我想打在Android按钮样式有两个背景颜色,如下面的图片:
I want to make a button style on Android with two background colors, such as the following image:
http://i.stack.imgur.com/ExKXl.png
时有可能使与绘图资源?从来就正在寻找在 http://developer.android的解决方案。 COM /引导/主题/资源/绘-resource.html 但没有人可以有两种颜色。
Is it possible to make with drawable resources? I´ve being searching for a solution on http://developer.android.com/guide/topics/resources/drawable-resource.html but none of them can have two colors.
有没有办法?
[编辑答案】
该解决方案是创建一个<层列表>
与项目和每个<项目>
有有一个<形状和GT;
。在code是波纹管(整个按钮具有32dp的高度,所以我用半高每种颜色):
The solution was to create a <layer-list>
with items and each <item>
has one <shape>
. The code is bellow (the entire button has 32dp height so I used half-height for each color):
<?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="#FF000" /> <!-- 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
第二形状,但它didn't显示我的角落!因此,解决方案是使用的android:半径
(所有8个顶点变圆,该死!),并把另外两个项目克服了额外的角落。在结束的XML有这样的:
But I had another issue, I was trying to put corners on each shape. I tried to put android:topLeftRadius
and android:topRightRadius
on the first shape and android:bottomLeftRadius
and android:bottomRightRadius
on the second shape but it didn´t show me the corners! So the solution was to use android:radius
(all the 8 corners became rounded, dammit!) and put another two items to overcome the extra corners. At the end the XML has like that:
<?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="#FF000" /> <!-- RED -->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="8dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#FF000" /> <!-- RED -->
</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 -->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="16dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00" /> <!-- GREEN -->
</shape>
</item>
</layer-list>
这是现在的工作,谢谢大家!
It is working now, thanks you all!
推荐答案
可能重复:<一href=\"http://stackoverflow.com/questions/8727238/banded-background-with-two-colors/8731177#8731177\">banded背景,两种颜色?
在其中使用Java提供的答案是:
In which a provided answer using Java:
Bitmap bmResult = Bitmap.createBitmap(buttonWidth, buttonHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmResult);
Paint paint = new Paint();
paint.setShader(new LinearGradient (0, 0, 0, bmResult.getHeight()/2, 0xFF284560, 0xFF284060, TileMode.MIRROR));
canvas.drawPaint(paint);
paint.setShader(new LinearGradient (0, 0, 0, bmResult.getHeight()/2, 0x55FFFFFF, 0x22FFFFFF, TileMode.CLAMP));
paint.setMaskFilter(new BlurMaskFilter(3, BlurMaskFilter.Blur.NORMAL));
canvas.drawRect(0, 0, bmResult.getWidth(), bmResult.getHeight()/2, paint)
从另一个解除SO张贴关于同一主题:上的按钮渐变和阴影
和使用XML绘接受的解决方案:
And the accepted solution using an XML drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#ff0000" />
</shape>
</item>
<item android:top="20dp">
<shape android:shape="rectangle" >
<size android:height="20dp" />
<solid android:color="#0000ff" />
</shape>
</item>
</layer-list>
和如其他人说:
您可以创建 9patch图片这将最足智多谋。
You can create a 9patch image which would be the most resourceful.
这篇关于安卓按钮有两个背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!