gpt4 book ai didi

Android:具有两种背景颜色的按钮

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

我想在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:topLeftRadiusandroid:topRightRadius在第一个形状和android:bottomLeftRadiusandroid: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>

现在可以使用了,谢谢大家!

最佳答案

可能重复:banded background with two colors?

其中使用 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 帖子:Gradients and shadows on buttons

以及使用 XML 可绘制对象的公认解决方案:

<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 image,这将是最足智多谋的。

关于Android:具有两种背景颜色的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8780119/

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