Android 自定义View画一条线

自定义View代码:

public class NavBarBackgroundView extends View {

    private Paint mPaint = new Paint();

    public NavBarBackgroundView(Context context) {
        this(context,null);
    }

    public NavBarBackgroundView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint.setColor(Color.parseColor("#ebebeb"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0,0,getWidth(),1,mPaint);
    }
}

xml文件中引用

<com.android.NavBarBackgroundView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00ff00"/>

在代码中引用:

 View view = new NavBarBackgroundView(mContext);

你可能感兴趣的:(Android 自定义View画一条线)