我正在以编程方式创建一个按钮。它是圆形的,具有渐变背景,工作正常且看起来不错,但我无法做我想做的两件事:
- 使用给定颜色设置 1 像素描边。我尝试了 getPaint().setStroke(),但无法弄清楚如何设置描边颜色。我该怎么做呢?
- 将渐变与按钮底部对齐,无论按钮的高度是多少。这可能吗?
作为参考,这是我正在使用的代码:
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);