我正在编写一个程序创建按钮。它是圆角的,有渐变背景,功能正常且外观漂亮,但我无法做到以下两点:
- 设置一个给定颜色的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);