Android增强的LinearLayout,带分隔线

来源:互联网 发布:淘宝装修队模板免费 编辑:程序博客网 时间:2024/05/21 22:49

Android增强的LinearLayout,带分隔线.类似ListView,但是不支持Adapter.


/** * 增强的线性布局<BR> * 1.添加分隔线功能 *  * @author Hxuejie hxuejie@126.com */public class SuperLinearLayout extends LinearLayout {private Drawabledivider;private intdividerSize;private booleanisHeadDivider;private booleanisEndDivider;private StringdoubleDividers;private intdoubleDividerSpace;private List<View>dividerList;public SuperLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);dividerList = new ArrayList<View>();TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SuperLinearLayout);final int N = a.getIndexCount();for (int i = 0; i < N; i++) {int attr = a.getIndex(i);switch (attr) {case R.styleable.SuperLinearLayout_divider:divider = a.getDrawable(attr);break;case R.styleable.SuperLinearLayout_dividerSize:dividerSize = a.getDimensionPixelSize(attr, 1);break;case R.styleable.SuperLinearLayout_dividerHead:isHeadDivider = a.getBoolean(attr, true);break;case R.styleable.SuperLinearLayout_dividerEnd:isEndDivider = a.getBoolean(attr, true);break;case R.styleable.SuperLinearLayout_dividerDouble:doubleDividers = a.getString(attr);break;case R.styleable.SuperLinearLayout_dividerDoubleSize:doubleDividerSpace = a.getDimensionPixelSize(attr, 5);break;}}a.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {if (divider == null) {return;}// clearremoveAllDividers();// add dividersint count = getChildCountByVisible();if (count == 0) {return;}int index = 1;// Headif (isHeadDivider) {insertDivider(0, 0);++index;}// Betweenfor (int i = 1; i < count; ++i, ++index) {insertDivider(index++, 0);if (isDoubleDivider(i)) {insertDivider(index++, doubleDividerSpace);}}// Endif (isEndDivider) {insertDivider(-1, 0);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);// reset divider size int mh, mw, dw, dh;for (View v : dividerList) {switch (getOrientation()) {case HORIZONTAL:dw = dividerSize;dh = getMeasuredHeight();break;default:case VERTICAL:dw = getMeasuredWidth();dh = dividerSize;break;}v.getLayoutParams().width = dw;v.getLayoutParams().height = dh;mh = MeasureSpec.makeMeasureSpec(dh, MeasureSpec.EXACTLY);mw = MeasureSpec.makeMeasureSpec(dw, MeasureSpec.EXACTLY);v.measure(mw, mh);}}/** * 得到可显示的子控件的数量 *  * @return */private int getChildCountByVisible() {int count = 0;for (int i = 0; i < getChildCount(); ++i) {if (getChildAt(i).getVisibility() != View.VISIBLE) {continue;}++count;}return count;}/** * 插入分隔线 *  * @param index * @param space */private void insertDivider(int index, int space) {View divider = createDivider();LayoutParams lp = createDividerLayoutParams();lp.topMargin = space;addViewInLayout(divider, index, lp);}/** * 删除所有分隔线 */private void removeAllDividers() {Iterator<View> it = dividerList.iterator();while (it.hasNext()) {View v = it.next();removeView(v);it.remove();}}/** * 检测插入的位置是否为双分隔线 *  * @param index 插入位置 * @return */private boolean isDoubleDivider(int index) {return doubleDividers.contains(String.valueOf(index));}/** * 创建一条分隔线,并添加到分隔线列表 *  * @return */private View createDivider() {View dv = new View(getContext());dv.setBackground(divider);dividerList.add(dv);return dv;}private LayoutParams createDividerLayoutParams() {LayoutParams lp;switch (getOrientation()) {case HORIZONTAL:lp = new LayoutParams(1, dividerSize);break;default:case VERTICAL:lp = new LayoutParams(dividerSize, 1);break;}return lp;}}
-------------------------------------------------------------------------

XML自定义属性:

<declare-styleable name="SuperLinearLayout">        <attr name="divider" format="reference|color" />        <attr name="dividerSize" format="dimension" />        <attr name="dividerHead" format="boolean" />        <attr name="dividerEnd" format="boolean" />        <attr name="dividerDouble" format="string" />        <attr name="dividerDoubleSize" format="dimension" /></declare-styleable>


0 0