两列布局



 import
java.lang.Math; 
import android.view.View; 
import android.view.ViewGroup; 
import android.content.Context; 
import android.util.AttributeSet; 
 
public class SideLayout extends ViewGroup { 
 
   
private int max_wid, max_hgt; 
 
   
public SideLayout(Context ctx) { 
     
super(ctx); 
   
} 
 
   
public SideLayout(Context ctx, AttributeSet attrs) { 
     
super(ctx, attrs); 
   
} 
 
   
public SideLayout(Context ctx, AttributeSet attrs, int defStyle) { 
     
super(ctx, attrs, defStyle); 
   
} 
 
   
@Override 
   
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
   
{ 
       
final int num_children = getChildCount(); 
 
       
// Since this is a special-purpose widget, this is brute simple.  All 
       
// of our children will have sizes of wrap_content, so we query them 
       
// all, then make a second pass assigning their final sizes. 
 
        max_wid
= 0; 
        max_hgt
= 0; 
       
for( int i = 0; i < num_children; ++i ) 
       
{ 
           
final View child = getChildAt(i); 
           
if( child != null && child.getVisibility() != View.GONE ) 
           
{ 
                measureChild
(child, widthMeasureSpec, heightMeasureSpec); 
                max_wid
= Math.max(max_wid, child.getMeasuredWidth()); 
                max_hgt
= Math.max(max_hgt, child.getMeasuredHeight()); 
           
} 
       
} 
 
       
// Make a second pass, tell children the actual size they got. 
       
for( int i = 0; i < num_children; ++i ) 
       
{ 
           
final View child = getChildAt(i); 
           
if( child != null && child.getVisibility() != View.GONE ) 
           
{ 
                child
.measure( 
                 
MeasureSpec.makeMeasureSpec(max_wid, MeasureSpec.EXACTLY), 
                 
MeasureSpec.makeMeasureSpec(max_hgt, MeasureSpec.EXACTLY)); 
           
} 
       
} 
 
       
super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
   
} 
 
   
@Override 
   
protected void onLayout(boolean changed, int l, int t, int r, int b) 
   
{ 
       
final int num_children = getChildCount(); 
       
int x,y; 
 
       
if( num_children <= 0 ) 
         
return; 
 
       
// Split the children into two groups, left and right.  Each 
       
// column is evenly-spaced. 
 
       
int wid = r - l; 
       
int hgt = b - t; 
       
int nrow = (num_children + 1) / 2; 
       
int margin = (hgt - max_hgt * nrow) / (nrow + 1); 
 
       
int i = 0; 
       
for( int col = 0; col < 2; ++col ) { 
            x
= col == 0 ? 0 : wid - max_wid; 
            y
= margin; 
           
for( int row = 0; row < nrow; ++row, ++i ) { 
               
if( i < num_children ) { 
                   
final View child = getChildAt(i); 
                   
if( child != null && child.getVisibility() != View.GONE ) 
                      child
.layout(x, y, x+max_wid, y+max_hgt); 
               
} 
                y
+= margin + max_hgt; 
           
}    
       
}    
   
}    
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值