自定义自动换行的线性布局linearLayout

由于前段时间项目中使用到了自动换行的线性布局,本来打算用表格布局在里面一个个的用Java代码添加ImageView的,但是添加的View控件是不确定的,因为得靠服务器的数据返回,就这样手动用Java代码画布局的方式就这样夭折了,因为在表哥布局中我无法确定一行显示多少个ImageView的数目,所以无法动态添加,最后自能自己去看看那种能够换行的线性布局了,线性布局比较不好的是不能自动换行,也就是当设置LinearLayout的orentation 设置为vertical 为竖直方向也就是只有一列,每行只能显示一个View或者View的子类,当设置LinearLayout的orentitation为Horizontal,LinearLayout的只能显示为一行,横向显示,当屏幕满了的时候,View控件并不会自动换行,所以我们要做的就是在LinearLayout满的时候自动换行。

需要了解的是怎么样绘制根据子控件的长宽绘制父控件的宽度与高度,所以需要传入的参数控件的高度,视图分为两种一种是View类型的,代表控件有TextView,Button,EditText 等等,还有一种是装视图的容器控件继承自ViewGroup的控件,如LinearLayout,RelativeLayout,TabHost等等控件,需要自动换行的线性布局的话,就需要根据子控件的高度与宽度,来动态加载父控件的高度与宽度,所以需要在构造函数中传入每一个子控件的固定的高度,或者是动态设置子控件的高度与宽度。

将自定义的LinearLayout 也继承自ViewGroup 并且重写抽象类ViewGrouop的几个方法:onMeasure(),onLayout(),dispathDraw()  三个方法的意思分别是:第一个onMeasure()是用来计算控件以及子控件所占用的区域,第二个onLayout()是控制子控件的换行,第三个可写可不写,主要是用来绘制控件的边框,

自定义LinearLayout的代码如下:

[java]  view plain  copy
 print ?
  1. package com.huanglong.mylinearlayout;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Rect;  
  8. import android.util.AttributeSet;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11.   
  12. /** 
  13.  * @author huanglong 2013-5-28 自定义自动换行LinearLayout 
  14.  */  
  15. public class FixGridLayout extends ViewGroup {  
  16.     private int mCellWidth;  
  17.     private int mCellHeight;  
  18.   
  19.     public FixGridLayout(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     public FixGridLayout(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.   
  27.     public FixGridLayout(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.     }  
  30.   
  31.     public void setmCellWidth(int w) {  
  32.         mCellWidth = w;  
  33.         requestLayout();  
  34.     }  
  35.   
  36.     public void setmCellHeight(int h) {  
  37.         mCellHeight = h;  
  38.         requestLayout();  
  39.     }  
  40.   
  41.     /** 
  42.      * 控制子控件的换行 
  43.      */  
  44.     @Override  
  45.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  46.         int cellWidth = mCellWidth;  
  47.         int cellHeight = mCellHeight;  
  48.         int columns = (r - l) / cellWidth;  
  49.         if (columns < 0) {  
  50.             columns = 1;  
  51.         }  
  52.         int x = 0;  
  53.         int y = 0;  
  54.         int i = 0;  
  55.         int count = getChildCount();  
  56.         for (int j = 0; j < count; j++) {  
  57.             final View childView = getChildAt(j);  
  58.             // 获取子控件Child的宽高  
  59.             int w = childView.getMeasuredWidth();  
  60.             int h = childView.getMeasuredHeight();  
  61.             // 计算子控件的顶点坐标  
  62.             int left = x + ((cellWidth - w) / 2);  
  63.             int top = y + ((cellHeight - h) / 2);  
  64.             // int left = x;  
  65.             // int top = y;  
  66.             // 布局子控件  
  67.             childView.layout(left, top, left + w, top + h);  
  68.   
  69.             if (i >= (columns - 1)) {  
  70.                 i = 0;  
  71.                 x = 0;  
  72.                 y += cellHeight;  
  73.             } else {  
  74.                 i++;  
  75.                 x += cellWidth;  
  76.   
  77.             }  
  78.         }  
  79.     }  
  80.   
  81.     /** 
  82.      * 计算控件及子控件所占区域 
  83.      */  
  84.     @Override  
  85.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  86.         // 创建测量参数  
  87.         int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth, MeasureSpec.AT_MOST);  
  88.         int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight, MeasureSpec.AT_MOST);  
  89.         // 记录ViewGroup中Child的总个数  
  90.         int count = getChildCount();  
  91.         // 设置子空间Child的宽高  
  92.         for (int i = 0; i < count; i++) {  
  93.             View childView = getChildAt(i);  
  94.             /* 
  95.              * 090 This is called to find out how big a view should be. 091 The 
  96.              * parent supplies constraint information in the width and height 
  97.              * parameters. 092 The actual mesurement work of a view is performed 
  98.              * in onMeasure(int, int), 093 called by this method. 094 Therefore, 
  99.              * only onMeasure(int, int) can and must be overriden by subclasses. 
  100.              * 095 
  101.              */  
  102.             childView.measure(cellWidthSpec, cellHeightSpec);  
  103.         }  
  104.         // 设置容器控件所占区域大小  
  105.         // 注意setMeasuredDimension和resolveSize的用法  
  106.         setMeasuredDimension(resolveSize(mCellWidth * count, widthMeasureSpec),  
  107.                 resolveSize(mCellHeight * count, heightMeasureSpec));  
  108.         // setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);  
  109.   
  110.         // 不需要调用父类的方法  
  111.         // super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  112.     }  
  113.   
  114.     /** 
  115.      * 为控件添加边框 
  116.      */  
  117.     @Override  
  118.     protected void dispatchDraw(Canvas canvas) {  
  119.         // 获取布局控件宽高  
  120.         int width = getWidth();  
  121.         int height = getHeight();  
  122.         // 创建画笔  
  123.         Paint mPaint = new Paint();  
  124.         // 设置画笔的各个属性  
  125.         mPaint.setColor(Color.BLUE);  
  126.         mPaint.setStyle(Paint.Style.STROKE);  
  127.         mPaint.setStrokeWidth(10);  
  128.         mPaint.setAntiAlias(true);  
  129.         // 创建矩形框  
  130.         Rect mRect = new Rect(00, width, height);  
  131.         // 绘制边框  
  132.         canvas.drawRect(mRect, mPaint);  
  133.         // 最后必须调用父类的方法  
  134.         super.dispatchDraw(canvas);  
  135.     }  
  136.   
  137. }  
然后在Xml文件中引用自己定义的控件,在Java代码中调用:

[java]  view plain  copy
 print ?
  1. package com.huanglong.mylinearlayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.widget.CheckBox;  
  8. import android.widget.SimpleAdapter;  
  9. import android.support.v4.app.NavUtils;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private SimpleAdapter adapter;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         FixGridLayout fixGridLayout = (FixGridLayout) findViewById(R.id.ll);  
  18.         fixGridLayout.setmCellHeight(30);  
  19.         fixGridLayout.setmCellWidth(100);  
  20.         for (int i = 0; i < 7; i++) {  
  21.             CheckBox box = new CheckBox(MainActivity.this);  
  22.             box.setText("第"+i+"个");  
  23.             fixGridLayout.addView(box);  
  24.         }  
  25.     }  
  26.   
  27.     @Override  
  28.     public boolean onCreateOptionsMenu(Menu menu) {  
  29.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  30.         return true;  
  31.     }  
  32.   
  33.       
  34. }  

效果截图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值