读书人

自定义绝对格局的实现

发布时间: 2012-06-29 15:48:46 作者: rapoo

自定义绝对布局的实现

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);}@Overrideprotected ViewGroup.LayoutParams generateDefaultLayoutParams() {return new MyAbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 0, 0);}@Overrideprotected 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));}@Overrideprotected 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());}}}@Overrideprotected boolean checkLayoutParams(ViewGroup.LayoutParams p) {return p instanceof MyAbsoluteLayout.LayoutParams;}@Overrideprotected 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);}}}

?

读书人网 >移动开发

热点推荐