Android 尝试绘制按钮:如何设置笔触颜色以及如何在不知道高度的情况下将渐变“对齐”到底部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2145131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to draw a button: how to set a stroke color and how to "align" a gradient to the bottom without knowing the height?
提问by moraes
I am creating a button programmatically. It is rounded and has a gradient background, and works fine and looks nice, but I couldn't do two things I wanted:
我正在以编程方式创建一个按钮。它是圆形的,有渐变背景,工作正常,看起来不错,但我不能做我想做的两件事:
- Set a 1 pixel stroke with a given color. I tried getPaint().setStroke(), but couldn't figure how to set the stroke color. How should I do it?
- Align the gradient to the bottom of the button, no matter what height it has. Is this possible?
- 使用给定的颜色设置 1 像素的笔划。我尝试了 getPaint().setStroke(),但不知道如何设置笔触颜色。我该怎么做?
- 将渐变与按钮的底部对齐,无论它有什么高度。这可能吗?
For reference, this is the code I'm using:
作为参考,这是我正在使用的代码:
Button btn = new Button(context);
btn.setPadding(7, 3, 7, 5);
btn.setTextColor(text_color);
// Create a gradient for the button. Height is hardcoded to 30 (I don't know the height beforehand).
// I wish I could set the gradient aligned to the bottom of the button.
final Shader shader = new LinearGradient(0, 0, 0, 30,
new int[] { color_1, color_2 },
null, Shader.TileMode.MIRROR);
float[] roundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }
ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));
normal.getPaint().setShader(shader);
normal.setPadding(7, 3, 7, 5);
// Create a state list (I suppressed settings for pressed).
StateListDrawable state_list = new StateListDrawable();
state_list.addState(new int[] { }, normal);
btn.setBackgroundDrawable(state_list);
回答by Stev_k
In terms of your first question, I struggled with this as well, and it doesn't look like there are any suitable methods within Drawables themselves (I was using ShapeDrawable
) or the Paint class. However, I was able to extend ShapeDrawable
and override the draw method, as below, to give the same result:
就您的第一个问题而言,我也为此苦苦挣扎,而且 Drawables 本身(我正在使用ShapeDrawable
)或 Paint 类中似乎没有任何合适的方法。但是,我能够扩展ShapeDrawable
和覆盖 draw 方法,如下所示,以给出相同的结果:
public class CustomBorderDrawable extends ShapeDrawable {
private Paint fillpaint, strokepaint;
private static final int WIDTH = 3;
public CustomBorderDrawable(Shape s) {
super(s);
fillpaint = this.getPaint();
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(WIDTH);
strokepaint.setARGB(255, 0, 0, 0);
}
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint) {
shape.draw(canvas, fillpaint);
shape.draw(canvas, strokepaint);
}
public void setFillColour(int c){
fillpaint.setColor(c);
}
}
回答by garmax1
You can use a GradientDrawable with setStroke(3, Color.WHITE) method. To make rounded corners, use this:
您可以将 GradientDrawable 与 setStroke(3, Color.WHITE) 方法一起使用。要制作圆角,请使用以下命令:
setShape(GradientDrawable.RECTANGLE);
setCornerRadii(new float[]{2,2,2,2,2,2,2,2});
回答by jigar
Try this....in drawable make a new xml file and set it where you want it..!
试试这个....在 drawable 中创建一个新的 xml 文件并将其设置在您想要的位置..!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#e1e1e1" />
<stroke
android:width="2dp"
android:color="#808080" />
<corners android:radius="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>