ITeye博客

LinearLayout 自动换行

xixinfei · 2014-08-22 16:05 · Android · 移动开发 · Markdown

由于前段时间项目中使用 到了自动换行的线性布局,本来打算用表格布局在里面一个个的用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 plaincopyprint?

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

然后在Xml文件中引用自己定义的控件,在Java代码中调用:

 

[java] view plaincopyprint?

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

 

效果截图:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。