Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
带上下分割线的 LinearLayout
package com.parkingwang.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import com.parkingwang.app.R;
/**
* 带边界线的LinearLayout
*
* @author Geek_Soledad (msdx.android@qq.com)
* @version 2015-11-12
* @since 2015-11-12
*/
public class BorderLinearLayout extends LinearLayout {
private static final int TOP = 0x1;
private static final int BOTTOM = 0x2;
private int mBorder;
private int mBorderWidth;
private int mBorderColor;
private final Paint mPaint;
public BorderLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BorderLinearLayout);
mBorder = a.getInt(R.styleable.BorderLinearLayout_pwBorder, 0);
mBorderWidth = a.getDimensionPixelSize(R.styleable.BorderLinearLayout_pwBorderWidth, 1);
mBorderColor = a.getColor(R.styleable.BorderLinearLayout_pwBorderColor, getResources().getColor(R.color.divider));
a.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(mBorderColor);
mPaint.setStyle(Paint.Style.FILL);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if ((mBorder & TOP) > 0) {
canvas.drawRect(0, 0, getWidth(), mBorderWidth, mPaint);
}
if ((mBorder & BOTTOM) > 0) {
canvas.drawRect(0, getHeight() - mBorderWidth, getWidth(), getHeight(), mPaint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.