Skip to content

Instantly share code, notes, and snippets.

@msdx
Created April 13, 2016 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msdx/d87b1bc70a12af2f94cf40f98775aabe to your computer and use it in GitHub Desktop.
Save msdx/d87b1bc70a12af2f94cf40f98775aabe to your computer and use it in GitHub Desktop.
带上下分割线的 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