ITeye博客

自定义绝对布局的实现

yangzc106 · 2012-01-05 22:01 · Android · 移动开发 · Markdown

android 绝对布局已经过期了为了保证稳定性自定义了一个绝对布局

 

package cn.kuwo.base.skin.view;

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

public class MyAbsoluteLayout extends ViewGroup {

	private int mPaddingLeft;
	private int mPaddingRight;
	private int mPaddingTop;
	private int mPaddingBottom;

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

	public MyAbsoluteLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaddingLeft = attrs
				.getAttributeIntValue(android.R.attr.paddingLeft, 0);
		mPaddingRight = attrs.getAttributeIntValue(android.R.attr.paddingRight,
				0);
		mPaddingTop = attrs.getAttributeIntValue(android.R.attr.paddingTop, 0);
		mPaddingBottom = attrs.getAttributeIntValue(
				android.R.attr.paddingBottom, 0);
	}

	public MyAbsoluteLayout(Context context) {
		super(context);
	}

	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new MyAbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, 0, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int count = getChildCount();

		int maxHeight = 0;
		int maxWidth = 0;

		measureChildren(widthMeasureSpec, heightMeasureSpec);

		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() != GONE) {
				int childRight;
				int childBottom;

				MyAbsoluteLayout.LayoutParams lp = (MyAbsoluteLayout.LayoutParams) child
						.getLayoutParams();

				childRight = lp.x + child.getMeasuredWidth();
				childBottom = lp.y + child.getMeasuredHeight();

				maxWidth = Math.max(maxWidth, childRight);
				maxHeight = Math.max(maxHeight, childBottom);
			}
		}

		maxWidth += mPaddingLeft + mPaddingRight;
		maxHeight += mPaddingTop + mPaddingBottom;

		maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
		maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

		setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
				resolveSize(maxHeight, heightMeasureSpec));
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int count = getChildCount();

		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() != GONE) {

				MyAbsoluteLayout.LayoutParams lp = (MyAbsoluteLayout.LayoutParams) child
						.getLayoutParams();

				int childLeft = mPaddingLeft + lp.x;
				int childTop = mPaddingTop + lp.y;
				child.layout(childLeft, childTop, childLeft
						+ child.getMeasuredWidth(), childTop
						+ child.getMeasuredHeight());

			}
		}
	}

	@Override
	protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
		return p instanceof MyAbsoluteLayout.LayoutParams;
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(
			ViewGroup.LayoutParams p) {
		return new MyAbsoluteLayout.LayoutParams(p);
	}

	public static class LayoutParams extends ViewGroup.LayoutParams {

		public int x;
		public int y;
		public int width;
		public int height;

		public LayoutParams(int x, int y, int width, int height) {
			super(width, height);
			this.x = x;
			this.y = y;
			this.width = width;
			this.height = height;
		}

		public LayoutParams(Context context, AttributeSet attrset) {
			super(context, attrset);
		}

		public LayoutParams(int width, int height) {
			super(width, height);
		}

		public LayoutParams(ViewGroup.LayoutParams params) {
			super(params);
		}

	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。