Trying to draw a button: how to set a stroke color and how to “align” a gradient to the bottom without knowing the height?

前端 未结 3 1039
轮回少年
轮回少年 2020-11-30 06:45

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:

  1. Se
相关标签:
3条回答
  • 2020-11-30 07:36

    Try this....in drawable make a new xml file and set it where you want it..!

    <?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>
    
    0 讨论(0)
  • 2020-11-30 07:37

    You can use a GradientDrawable with setStroke(3, Color.WHITE) method. To make rounded corners, use this:

    setShape(GradientDrawable.RECTANGLE);
    setCornerRadii(new float[]{2,2,2,2,2,2,2,2});
    
    0 讨论(0)
  • 2020-11-30 07:44

    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:

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题