Android 自定义View画一条线

来源:互联网 发布:淘宝首页宝贝展示框架 编辑:程序博客网 时间:2024/06/05 03:59

自定义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);
原创粉丝点击