public class PredicateLayout extends ViewGroup { private int line_height; public static final int DEFAULT_HORIZONTAL_SPACING = 5; public static final int DEFAULT_VERTICAL_SPACING = 5; private int horizontalSpacing; private int verticalSpacing; public PredicateLayout(Context context) { super(context); } public PredicateLayout(Context context, int horizontalSpacing, int verticalSpacing) { super(context); this.horizontalSpacing = horizontalSpacing; this.verticalSpacing = verticalSpacing; } public PredicateLayout(Context context, AttributeSet attrs) { super(context, attrs); //TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.PredicateLayout); //horizontalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.PredicateLayout_item_h_space, DEFAULT_HORIZONTAL_SPACING); //verticalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.PredicateLayout_item_v_space, DEFAULT_VERTICAL_SPACING); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED); final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); final int count = getChildCount(); int line_height = 0; // int line_height = 22; int xpos = getPaddingLeft(); int ypos = getPaddingTop(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { child.measure(MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED)); final int childw = child.getMeasuredWidth(); line_height = Math.max(line_height, child.getMeasuredHeight() + verticalSpacing); if (xpos + childw > width) { xpos = getPaddingLeft(); ypos += line_height; } xpos += childw + horizontalSpacing; } } this.line_height = line_height; if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) { height = ypos + line_height; } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { if (ypos + line_height < height) { height = ypos + line_height; } } setMeasuredDimension(width, height); } @Override protected ViewGroup.LayoutParams generateDefaultLayoutParams() { return new LayoutParams(1, 1); // default of 1px spacing } @Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof LayoutParams; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int count = getChildCount(); final int width = r - l; int xpos = getPaddingLeft(); int ypos = getPaddingTop(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final int childw = child.getMeasuredWidth(); final int childh = child.getMeasuredHeight(); // final int childh = 24; if (xpos + childw > width) { xpos = getPaddingLeft(); ypos += line_height; } child.layout(xpos, ypos, xpos + childw, ypos + childh); xpos += childw + horizontalSpacing; } } } }
相关推荐
Android 标签,可以自动换行的ViewGroup
多标签自动换行的控件ViewGroup ;
可自动换行效果的viewgroup,支持一键多选,单选,绝对是多标签选择的不二选择
实用的自动换行的LinearLayout
可以自动换行,如果一行放不下,可换行,还可以支持滚动布局。
Android基于ViewGroup实现自动换行标签控件。
解决自动换行,自适应高度。常见与标签样式的 ViewGroup。很容易赵东自动换行的demo,但是很多都没有自适应高度,本人找了2个小时终于找到方法。
android 通过自定义ViewGroup实现自动换行的流布局,支持padding与margin
自定义ViewGroup实现自动换行功能,一行控件填充满以后自动换到下一行。
今天看到很多app都有Title自动隐藏功能,1.这是一个Gradle工程,Gradle的版本是2.10,项目的Compile sdk Version是 23 , Build ...Android自定义ViewGroup实现Title自动隐藏功能源代码分享,Android开发者必看示例。
在我们开发过程中会经常遇见一些客户要求但是Android系统又不提供的效果,这时我们只能自己动手去实现它,或者从网络上借鉴他人的资源,本着用别人不如自己会做的心态,在此我总结了一下Android中如何实现自动换行的...
一些搜索软件下面一般都会提供一些关键字供用户参考,该控件可以根据关键字多少实现自动换行
自定义ViewGroup 实现类似于电商平台上的商品属性显示效果
今天看到很多app都有Title自动隐藏功能,自己尝试写了一个最简单版本的。最后还封装了一下,喜欢的朋友可以下载工程看一下效果。
自定义ViewGroup实现view自动换行
使用ViewGroup实现微信左右滑动切换界面以及界面裂开的效果
AndroidTagGroup实现云标签效果,各种背景效果的标签,并且支持添加, 大体实现:有一个TagsManager管理TagGroup,自定义TagGroup...TagGroup extends ViewGroup,TagGroup 由多个TagView组成,TagView继承TextView 。
ViewGroup实现九宫格并可拖动位置
该资源包含了一个利用自定义的viewgroup实现的slidingmenu,简单易懂。