Created
April 13, 2016 03:25
-
-
Save msdx/d87b1bc70a12af2f94cf40f98775aabe to your computer and use it in GitHub Desktop.
带上下分割线的 LinearLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ([email protected]) | |
* @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