Android:具有两种背景颜色的按钮 [英] Android: button with two background colors
问题描述
我想在Android上使用两种背景颜色制作按钮样式,例如以下图片:
http://i.stack.imgur.com/ExKXl.png
它可以用可绘制资源?我正在寻找 http://developer.android上的解决方案。 com / guide / topics / resources / drawable-resource.html ,但是它们都不能有两种颜色。
有办法吗?
解决方案是创建一个< layer-list& / code>与项目和每个
< item>
有一个< shape>
。代码是下面的(整个按钮有32dp高度,所以我为每种颜色使用半高):
<?xml version =1.0encoding =utf-8?>
< layer-list xmlns:android =http://schemas.android.com/apk/res/android>
<! - 顶部颜色 - >
< item android:bottom =16dp>
< shape android:shape =rectangle>
< solid android:color =#FF0000/> <! - RED - >
< / shape>
< / item>
<! - 底部颜色 - >
< 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.0encoding =utf-8? >
< layer-list xmlns:android =http://schemas.android.com/apk/res/android>
<! - 顶部颜色带角 - >
< item android:bottom =16dp>
< shape android:shape =rectangle>
< corners android:radius =5dp/> <! - 这是强制性的,它不工作只与android:topLeftRadius和android:topRightRadius - >
< solid android:color =#FF0000/> <! - RED Color - >
< / shape>
< / item>
<! - 离开中心角 - >
< item android:top =8dpandroid:bottom =8dp>
< shape android:shape =rectangle>
< solid android:color =#FF0000/> <! - RED Color - >
< / shape>
< / item>
<! - 底部颜色带角 - >
< item android:top =16dp>
< shape android:shape =rectangle>
< corners android:radius =5dp/> <! - 这是强制性的,它不工作只与android:bottomLeftRadius和android:bottomRightRadius - >
< solid android:color =#00FF00/> <! - GREEN Color - >
< / shape>
< / item>
<! - 离开中心角 - >
< item android:top =16dpandroid:bottom =8dp>
< shape android:shape =rectangle>
< solid android:color =#00FF00/> <! - GREEN Color - >
< / shape>
< / item>
< / layer-list>
现在正在工作,谢谢大家!
解决方案可能重复:带状背景有两种颜色?
其中使用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 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图片足智多谋。
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
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.
Is there a way?
[editing the answer]
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="#FF0000" /> <!-- RED --> </shape> </item> <!-- Bottom color --> <item android:top="16dp"> <shape android:shape="rectangle"> <solid android:color="#00FF00" /> <!-- GREEN --> </shape> </item> </layer-list>
But I had another issue, I was trying to put corners on each shape. I tried to put
android:topLeftRadius
andandroid:topRightRadius
on the first shape andandroid:bottomLeftRadius
andandroid:bottomRightRadius
on the second shape but it didn´t show me the corners! So the solution was to useandroid: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="#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>
It is working now, thanks you all!
解决方案Possible Duplicate: banded background with two colors?
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)
was lifted from another SO post on the same topic: Gradients and shadows on buttons
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>
And as others have said:
You can create a 9patch image which would be the most resourceful.
这篇关于Android:具有两种背景颜色的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!