android定义组件自动换行
2013-02-22 17:27
225 查看
让容器中的组件自动换行,之前看过一个例子,但没有根据容器计算高度。
在此基础上修改了一点代码,以适应自动的高度计算,自己试了下,貌似还可以。
修正(去除左边和顶部的间隙,添加是否隐藏的判断):
当一行的第一个子元素超过宽度时,不进行换行处理
在此基础上修改了一点代码,以适应自动的高度计算,自己试了下,貌似还可以。
package com.example.exam; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; public class LineBreakLayout extends ViewGroup { private final static String TAG = "LineBreakLayout"; public LineBreakLayout(Context context) { super(context); } public LineBreakLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LineBreakLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int mWidth = MeasureSpec.getSize(widthMeasureSpec); int mCount = getChildCount(); int mX = 0; int mY = 0; int mRow = 0; for (int index = 0; index < mCount; index++) { final View child = getChildAt(index); child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); // 此处增加onlayout中的换行判断,用于计算所需的高度 int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); mX += width; mY = mRow * height + height; if (mX > mWidth) { mX = width; mRow++; mY = mRow * height + height; } } // 设置容器所需的宽度和高度 setMeasuredDimension(mWidth, mY); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int count = getChildCount(); t = 0; int mX = l; int mY = t; int mRow = 0; for (int i = 0; i < count; i++) { final View child = this.getChildAt(i); int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); mX += width; mY = mRow * height + height + t; if (mX > r) { mX = width + l; mRow++; mY = mRow * height + height + t; } child.layout(mX - width, mY - height, mX, mY); } } }
修正(去除左边和顶部的间隙,添加是否隐藏的判断):
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int maxWidth = MeasureSpec.getSize(widthMeasureSpec); int childCount = getChildCount(); int x = 0; int y = 0; int row = 0; for (int index = 0; index < childCount; index++) { final View child = getChildAt(index); if (child.getVisibility() != View.GONE) { child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); // 此处增加onlayout中的换行判断,用于计算所需的高度 int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); x += width; y = row * height + height; if (x > maxWidth) { x = width; row++; y = row * height + height; } } } // 设置容器所需的宽度和高度 setMeasuredDimension(maxWidth, y); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int childCount = getChildCount(); int maxWidth = r - l; int x = 0; int y = 0; int row = 0; for (int i = 0; i < childCount; i++) { final View child = this.getChildAt(i); if (child.getVisibility() != View.GONE) { int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); x += width; y = row * height + height; if (x > maxWidth) { x = width; row++; y = row * height + height; } child.layout(x - width, y - height, x, y); } } }
当一行的第一个子元素超过宽度时,不进行换行处理
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int maxWidth = MeasureSpec.getSize(widthMeasureSpec); int childCount = getChildCount(); int x = 0; int y = 0; int row = 0; int count = 0; for (int index = 0; index < childCount; index++) { final View child = getChildAt(index); if (child.getVisibility() != View.GONE) { child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); // 此处增加onlayout中的换行判断,用于计算所需的高度 int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); x += width; y = row * height + height; count++; if (x > maxWidth) { x = width; if (count > 1) { y = ++row * height + height; } else { y = row * height + height; count = 1; } } } } // 设置容器所需的宽度和高度 setMeasuredDimension(maxWidth, y + row * BOTTOM_MARGIN + 10); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int childCount = getChildCount(); int maxWidth = r - l; int x = 0; int y = 0; int row = 0; int count = 0; for (int i = 0; i < childCount; i++) { final View child = this.getChildAt(i); if (child.getVisibility() != View.GONE) { int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); x += width; y = row * height + height; count++; if (x > maxWidth) { x = width; if (count > 1) { y = ++row * height + height; } else { y = row * height + height; count = 1; } } child.layout(x - width, row * BOTTOM_MARGIN + y - height, x, row * BOTTOM_MARGIN + y); } } }
相关文章推荐
- Android自定义组件之自动换行View
- Android自定义组件之自动换行的TextView
- Android自定义组件之自动换行View
- Android自定义组件之自动换行View - 儒雅小生
- 【Android进阶】Android自定义组件之自动换行View,以TextView为例
- Android自定义组件之自动换行ViewGroup
- Android自定义组件之自动换行View - 儒雅小生
- Android自己定义组件系列【5】——高级实践(1)
- Android自定义控件实现标签的显示自动换行(一)
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- Android 可自动换行的单选 RadioGroupEx
- Eclipse中的自动换行-android开发
- Android Logcat输出为何能自动换行输出的原因以及多\n的作用
- android 遇到TextView 没到末尾 自动换行 可能是 全角半角混合
- android bitmap绘制文字自动换行
- Android 简单实现View自动换行(附源码)
- Android:认识R类、findViewById方法查找组件、@string查找字符、@color查找颜色、@drawable查找图片、@dimen某个组件尺寸定义、项目引入资源国际化
- Android TextView自动换行文字排版参差不齐的原因
- Android基础组件(一)自动完成控件AutoCompleteTextView