Androidカスタムビューに線を引く

1932 ワード

カスタム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);