自定义绝对布局的实现

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值