标签:int 存在 tco log finally 构建 ams util resources
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <View android:layout_width ="100dp" android:layout_height ="150dp" android:background ="#FF0000" /> <View android:layout_width ="100dp" android:layout_height ="150dp" android:layout_marginLeft ="30dp" android:layout_marginTop ="20dp" android:background ="#00FF00" /> <View android:layout_width ="100dp" android:layout_height ="150dp" android:layout_marginLeft ="60dp" android:layout_marginTop ="40dp" android:background ="#0000FF" /> </RelativeLayout>效果如图2:
<FrameLayout <!--自己定义命名空间,以便在下文中使用自己定义的属性--> xmlns:cascade ="http://schemas.android.com/apk/res/com.manning.androidhacks.hack003" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <com.manning.androidhacks.hack003.view.CascadeLayout android:layout_width ="fill_parent" android:layout_height ="fill_parent" cascade:horizontal_spacing ="30dp"<!--由于前面加入了cascade命名空间,所以此处能够使用自己定义属性--> cascade:vertical_spacing ="20dp" > <View android:layout_width ="100dp" android:layout_height ="150dp" cascade:layout_vertical_spacing ="90dp"<!--为子View加入的自己定义属性,将在本文第三部分用到--> android:background ="#FF0000" /> <View android:layout_width ="100dp" android:layout_height ="150dp" android:background ="#00FF00" /> <View android:layout_width ="100dp" android:layout_height ="150dp" android:background ="#0000FF" /> </com.manning.androidhacks.hack003.view.CascadeLayout> </FrameLayout>
<? xml version ="1.0" encoding= "utf-8" ?> <resources> <declare-styleable name= "CascadeLayout" > <attr name= "horizontal_spacing" format = "dimension" /> <attr name= "vertical_spacing" format = "dimension" /> </declare-styleable> </resources>
<?xml version ="1.0" encoding= "utf-8" ?
> <resources> <dimen name= "cascade_horizontal_spacing" >10dp</dimen> <dimen name= "cascade_vertical_spacing" >10dp</dimen> </resources>
public CascadeLayout (Context context, AttributeSet attrs) { super( context, attrs); TypedArray a = context .obtainStyledAttributes (attrs , R. styleable. CascadeLayout ); try { mHorizontalSpacing = a. getDimensionPixelSize( R. styleable. CascadeLayout_horizontal_spacing , getResources ().getDimensionPixelSize ( R. dimen. cascade_horizontal_spacing )); mVerticalSpacing = a. getDimensionPixelSize( R. styleable. CascadeLayout_vertical_spacing , getResources () .getDimensionPixelSize (R .dimen .cascade_vertical_spacing )); } finally { a .recycle (); }
public static class LayoutParams extends ViewGroup .LayoutParams { int x; int y; public LayoutParams( Context context , AttributeSet attrs) { super (context , attrs ); } public LayoutParams( int w , int h ) { super (w , h ); } }
@Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) { int width = 0; int height = getPaddingTop (); final int count = getChildCount (); for ( int i = 0; i < count; i++) { View child = getChildAt (i ); measureChild (child , widthMeasureSpec , heightMeasureSpec ); LayoutParams lp = (LayoutParams ) child .getLayoutParams (); width = getPaddingLeft () + mHorizontalSpacing * i; lp .x = width; lp .y = height; width += child .getMeasuredWidth (); height += mVerticalSpacing ; } width += getPaddingRight (); height += getChildAt (getChildCount () - 1). getMeasuredHeight () + getPaddingBottom (); setMeasuredDimension ( resolveSize( width, widthMeasureSpec ), resolveSize( height, heightMeasureSpec )); }
@Override protected void onLayout (boolean changed, int l , int t , int r , int b ) { final int count = getChildCount (); for ( int i = 0; i < count; i++) { View child = getChildAt (i ); LayoutParams lp = ( LayoutParams ) child .getLayoutParams (); child .layout (lp .x , lp .y , lp .x + child. getMeasuredWidth (), lp .y + child .getMeasuredHeight ()); } }
<declare-styleable name="CascadeLayout_LayoutParams"> <attr name="layout_vertical_spacing" format="dimension" /> </declare-styleable>
public LayoutParams (Context context, AttributeSet attrs) { super (context , attrs ); TypedArray a = context .obtainStyledAttributes (attrs , R. styleable. CascadeLayout_LayoutParams ); try { verticalSpacing = a .getDimensionPixelSize ( R .styleable .CascadeLayout_LayoutParams_layout_vertical_spacing , -1 ); } finally { a .recycle (); } }
@Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) { int width = getPaddingLeft (); int height = getPaddingTop (); int verticalSpacing ; final int count = getChildCount (); for ( int i = 0; i < count; i++) { verticalSpacing = mVerticalSpacing ; View child = getChildAt (i ); measureChild (child , widthMeasureSpec , heightMeasureSpec ); LayoutParams lp = ( LayoutParams ) child .getLayoutParams (); width = getPaddingLeft () + mHorizontalSpacing * i; lp .x = width; lp .y = height; if (lp .verticalSpacing >= 0 ) { verticalSpacing = lp .verticalSpacing ; } width += child .getMeasuredWidth (); height += verticalSpacing ; } width += getPaddingRight (); height += getChildAt (getChildCount () - 1). getMeasuredHeight () + getPaddingBottom (); setMeasuredDimension ( resolveSize( width, widthMeasureSpec ), resolveSize( height, heightMeasureSpec )); }
package com.manning.androidhacks.hack003.view; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import com.manning.androidhacks.hack003.R; public class CascadeLayout extends ViewGroup { private int mHorizontalSpacing; private int mVerticalSpacing; public CascadeLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CascadeLayout); try { mHorizontalSpacing = a.getDimensionPixelSize( R.styleable.CascadeLayout_horizontal_spacing, getResources().getDimensionPixelSize( R.dimen.cascade_horizontal_spacing)); mVerticalSpacing = a.getDimensionPixelSize( R.styleable.CascadeLayout_vertical_spacing, getResources() .getDimensionPixelSize(R.dimen.cascade_vertical_spacing)); } finally { a.recycle(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getPaddingLeft(); int height = getPaddingTop(); int verticalSpacing; final int count = getChildCount(); for (int i = 0; i < count; i++) { verticalSpacing = mVerticalSpacing; View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutParams lp = (LayoutParams) child.getLayoutParams(); width = getPaddingLeft() + mHorizontalSpacing * i; lp.x = width; lp.y = height; if (lp.verticalSpacing >= 0) { verticalSpacing = lp.verticalSpacing; } width += child.getMeasuredWidth(); height += verticalSpacing; } width += getPaddingRight(); height += getChildAt(getChildCount() - 1).getMeasuredHeight() + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); LayoutParams lp = (LayoutParams) child.getLayoutParams(); child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight()); } } @Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof LayoutParams; } @Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(getContext(), attrs); } @Override protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { return new LayoutParams(p.width, p.height); } public static class LayoutParams extends ViewGroup.LayoutParams { int x; int y; public int verticalSpacing; public LayoutParams(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CascadeLayout_LayoutParams); try { verticalSpacing = a .getDimensionPixelSize( R.styleable.CascadeLayout_LayoutParams_layout_vertical_spacing, -1); } finally { a.recycle(); } } public LayoutParams(int w, int h) { super(w, h); } } }
50个Android开发技巧(03 自己定义ViewGroup)
标签:int 存在 tco log finally 构建 ams util resources
原文地址:http://www.cnblogs.com/blfbuaa/p/6696317.html