[关闭]
@Beeder 2018-01-11T14:23:59.000000Z 字数 2841 阅读 1228

Android代码 自动换行的LinearLayout

android

···
直接食用,引用替换LinearLayout即可

  1. package com.denqin.tools.toollibrary.ui.widget;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.view.WindowManager;
  7. /**
  8. * Created by liwenfeng on 18-1-11.
  9. */
  10. public class AutoLinefeedLayout extends ViewGroup {
  11. private int defaultWidth;
  12. private int defaultheight;
  13. public AutoLinefeedLayout(Context context, AttributeSet attrs, int defStyle) {
  14. super(context, attrs, defStyle);
  15. }
  16. public AutoLinefeedLayout(Context context, AttributeSet attrs) {
  17. this(context, attrs, 0);
  18. }
  19. public AutoLinefeedLayout(Context context) {
  20. this(context, null);
  21. }
  22. @Override
  23. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  24. layoutHorizontal();
  25. }
  26. private void layoutHorizontal() {
  27. final int count = getChildCount();
  28. final int lineWidth = getMeasuredWidth() - getPaddingLeft()
  29. - getPaddingRight();
  30. int paddingTop = getPaddingTop();
  31. int childTop = 0;
  32. int childLeft = getPaddingLeft();
  33. int availableLineWidth = lineWidth;
  34. int maxLineHight = 0;
  35. for (int i = 0; i < count; i++) {
  36. final View child = getChildAt(i);
  37. if (child == null) {
  38. continue;
  39. } else if (child.getVisibility() != GONE) {
  40. final int childWidth = child.getMeasuredWidth();
  41. final int childHeight = child.getMeasuredHeight();
  42. final int childWidth = defaultWidth;
  43. final int childHeight = defaultheight;
  44. if (availableLineWidth < childWidth) {
  45. availableLineWidth = lineWidth;
  46. paddingTop = paddingTop + maxLineHight;
  47. childLeft = getPaddingLeft();
  48. maxLineHight = 0;
  49. }
  50. childTop = paddingTop;
  51. setChildFrame(child, childLeft, childTop, childWidth,
  52. childHeight);
  53. childLeft += childWidth;
  54. availableLineWidth = availableLineWidth - childWidth;
  55. maxLineHight = Math.max(maxLineHight, childHeight);
  56. }
  57. }
  58. }
  59. private void setChildFrame(View child, int left, int top, int width,
  60. int height) {
  61. child.layout(left, top, left + width, top + height);
  62. }
  63. @Override
  64. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  65. final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  66. int count = getChildCount();
  67. for (int i = 0; i < count; i++) {
  68. measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
  69. }
  70. if (heightMode == MeasureSpec.AT_MOST||heightMode == MeasureSpec.UNSPECIFIED) {
  71. final int width = MeasureSpec.getSize(widthMeasureSpec);
  72. super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(
  73. getDesiredHeight(width), MeasureSpec.EXACTLY));
  74. } else {
  75. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  76. }
  77. }
  78. private int getDesiredHeight(int width) {
  79. final int lineWidth = width - getPaddingLeft() - getPaddingRight();
  80. int availableLineWidth = lineWidth;
  81. int totalHeight = getPaddingTop() + getPaddingBottom();
  82. int lineHeight = 0;
  83. for (int i = 0; i < getChildCount(); i++) {
  84. View child = getChildAt(i);
  85. final int childWidth = child.getMeasuredWidth();
  86. final int childHeight = child.getMeasuredHeight();
  87. if (availableLineWidth < childWidth) {
  88. availableLineWidth = lineWidth;
  89. totalHeight = totalHeight + lineHeight;
  90. lineHeight = 0;
  91. }
  92. availableLineWidth = availableLineWidth - childWidth;
  93. lineHeight = Math.max(childHeight, lineHeight);
  94. }
  95. totalHeight = totalHeight + lineHeight;
  96. return totalHeight;
  97. }
  98. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注