Android自定义ViewGroup:实现简单的垂直方向线性布局(2)

Android自定义ViewGroup:实现简单的垂直方向线性布局(2)


附录文章1自定义了一个ViewGroup,该ViewGroup实现了一个线性布局,水平方向的。本文修改附录文章1中的MyLayout.java代码文件,作为演练,再次实现一个简单的线性布局,不过这次的线性布局是垂直方向上的。
附录文章1的全部代码均不用修改,只需要把MyLayout.java的某些地方代码调整,即可实现线性布局的垂直方向布局:

  1.   
  2. import android.content.Context;  
  3. import android.util.AttributeSet;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.   
  7. public class MyLayout extends ViewGroup {  
  8.   
  9.     public MyLayout(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.     }  
  12.   
  13.     // 度量全部子view要占用的空间,宽和高  
  14.     //onMeasure被Android系统调用是在onLayout之前  
  15.     @Override  
  16.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  17.   
  18.         //所有子view加起来总的Measured Dimension高度和宽度  
  19.         int measuredWidth = 0;  
  20.         int measuredHeight = 0;  
  21.   
  22.         int count = getChildCount();  
  23.         for (int i = 0; i < count; i++) {  
  24.             View v = getChildAt(i);  
  25.             if (v.getVisibility() != View.GONE) {  
  26.                 measureChild(v, widthMeasureSpec, heightMeasureSpec);  
  27.   
  28.                 //measuredWidth += v.getMeasuredWidth();  
  29.                 measuredWidth=Math.max(measuredWidth,v.getMeasuredWidth());  
  30.   
  31.                 measuredHeight += v.getMeasuredHeight();  
  32.                 //measuredHeight=Math.max(measuredHeight, v.getMeasuredHeight());  
  33.             }  
  34.         }  
  35.   
  36.         //仔细检查!不要疏忽掉一些padding的值  
  37.         measuredWidth += getPaddingLeft() + getPaddingRight();  
  38.         measuredHeight += getPaddingTop() + getPaddingBottom();  
  39.   
  40.         //可选  
  41.         //measuredWidth = Math.max(measuredWidth, getSuggestedMinimumWidth());  
  42.         //measuredHeight = Math.max(measuredHeight, getSuggestedMinimumHeight());  
  43.   
  44.         //另外一种set度量值的方法  
  45.         //setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec),resolveSize(measuredHeight, heightMeasureSpec));  
  46.   
  47.         setMeasuredDimension(measuredWidth, measuredHeight);  
  48.     }  
  49.   
  50.     //Android系统在onMeasure之后调用onLayout  
  51.     @Override  
  52.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  53.         //此时回调的参数l,t,r,b是上一步onMeasure计算出来的值。r是总宽度,b是总高度  
  54.         //我们在l,t,r,b这四个参数“框”出来的空间内一个一个摆放我们自己的子view  
  55.   
  56.         int count = getChildCount();  
  57.         for (int i = 0; i < count; i++) {  
  58.             View v = getChildAt(i);  
  59.             if (v.getVisibility() != View.GONE) {  
  60.                 int childWidth = v.getMeasuredWidth();  
  61.                 int childHeight = v.getMeasuredHeight();  
  62.   
  63.                 //开始摆放  
  64.                 v.layout(l, t, l + childWidth, t + childHeight);  
  65.   
  66.                 //把左边的锚定位置往右移  
  67.                 //如果在垂直方向继续累加l偏移量,那么显示出来的三个子view呈现阶梯状。  
  68.                 //l += childWidth;  
  69.   
  70.                 //垂直方向累计坐标量  
  71.                 t += childHeight;  
  72.             }  
  73.         }  
  74.     }  
  75. }  


代码运行结果:





如果把MyLayout里面的onMeasure里面的一段被注释掉的代码:
measuredWidth += v.getMeasuredWidth();
启用,同时在onLayout里面的一段被注释掉的代码:
l += childWidth;
启用,那么摆放出来的子view呈现阶梯状,如图所示:





附录文章:
1,《Android自定义ViewGroup:onMeasure与onLayout(1)》

链接地址:http://blog.csdn.net/qingxuan521721/article/details/78752864

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值