我正在以编程方式创建一个按钮.它是圆形的,有渐变背景,工作正常,看起来不错,但我不能做我想要的两件事:
作为参考,这是我正在使用的代码:
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);
Run Code Online (Sandbox Code Playgroud)
Ste*_*v_k 19
就你的第一个问题而言,我也在努力解决这个问题,而且看起来在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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将GradientDrawable与setStroke(3,Color.WHITE)方法一起使用.要制作圆角,请使用:
setShape(GradientDrawable.RECTANGLE);
setCornerRadii(new float[]{2,2,2,2,2,2,2,2});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27319 次 |
| 最近记录: |