自定义换行布局


自定义换行布局,直接上代码


package com.sbnnest.view;


import java.util.Hashtable;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;


/**
 * 
 * 自动换行布局
 * 
 * @author Alex.Lu
 * @version V1.0
 */
public class NewlineLayout extends ViewGroup {

	
	Hashtable<View, ViewPostion> mMap = new Hashtable<View, NewlineLayout.ViewPostion>();

	public NewlineLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

	}

	public NewlineLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public NewlineLayout(Context context) {
		super(context);
	}
	
	@Override
	public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
	{
		return new MarginLayoutParams(getContext(), attrs);
	}
	
	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		// TODO Auto-generated method stub
		return new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
	}	

	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			ViewPostion pos = mMap.get(child);
			if (pos != null) {
				child.layout(pos.getLeft(), pos.getTop(), pos.getRight(), pos.getBottom());
			} 
		}
	}
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int maxH = 0;
		int left, right, top, bottom;
		int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
		left = getPaddingLeft();
		top = getPaddingTop();
		right = 0;
		bottom = 0;
		int count = getChildCount();
		mMap.clear();
		for (int i = 0; i < count; i++) {
			
			View child = getChildAt(i);
			if(child.getVisibility() == View.GONE){
				continue;
			}
			
			MarginLayoutParams mp = (MarginLayoutParams) child.getLayoutParams();
			measureChildWithMargins(child, widthMeasureSpec, 0,heightMeasureSpec, 0);
			left = left + mp.leftMargin;
			right = left + child.getMeasuredWidth() + mp.rightMargin;
			maxH = Math.max(maxH, child.getMeasuredHeight());
			/**
			 * 如果大于宽度就
			 */
			if (right > width) {
				left = getPaddingLeft() + mp.leftMargin;
				right = left + child.getMeasuredWidth();
				top += maxH;
				maxH = 0;
			}
			bottom = top + child.getMeasuredHeight();
			ViewPostion position = new ViewPostion();
			position.setPostion(left, top, right, bottom);
			left = right;
			mMap.put(child, position);
		}
		
		setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bottom + getPaddingBottom());
	}
	

	
	/**
	 * 
	 * 存储布局位置信息
	 * 
	 * @author Alex.Lu
	 *
	 */
	private class ViewPostion {
		
		private int mLeft, mTop, mRight, mBottom;
		
		public void setPostion(int left,int top,int right,int bottom){
			mLeft = left;
			mTop = top;
			mRight =  right;
			mBottom = bottom;
		}

		public int getLeft() {
			return mLeft;
		}


		public int getTop() {
			return mTop;
		}


		public int getRight() {
			return mRight;
		}

		public int getBottom() {
			return mBottom;
		}

	}

}



1.先调用onMeasure方法,在测量每个布局的大小,并把位置给记录下来放在map里。

2.调用onLayout方法,把map中的每一个子组件进行布局。


注意需要重写generateLayoutParams的两个方法,否则会报错误。


demo下载地址:http://download.csdn.net/detail/workwall/9296097

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值