[自定义控件系列2]--进阶篇:可自动换行的ViewGroup
2017-04-09 21:07
302 查看
前言
上一篇文章介绍了简单的自定义viewGroup怎么实现[自定义控件系列]–自定义viewGourp
下面我们介绍一个可以自动换行的Viewgroup,这是一个很常见的需求,先看一下实现效果。
具体实现
package com.example.king.kingcustomview; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import static android.R.attr.width; import static android.view.View.MeasureSpec.getMode; /** * Created by king on 2017/4/9. * 博客地址:http://blog.csdn.net/jinfulin */ public class kingAutoLinearLayout extends ViewGroup { private int parentWidth; private int parentHeight; private int parentautoHeight = 0; public kingAutoLinearLayout(Context context) { this(context, null); } public kingAutoLinearLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public kingAutoLinearLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //没有用到的 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); parentWidth = MeasureSpec.getSize(widthMeasureSpec); parentHeight = MeasureSpec.getSize(heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); Log.e("king11",parentHeight+"========"+heightMode); for (int i = 0; i < getChildCount(); i++) { View childview = getChildAt(i); // 测量每一个child的宽和高 measureChild(childview, widthMeasureSpec, heightMeasureSpec); int childHight = childview.getMeasuredHeight(); } //如果高度是wrap_content设置为我们计算的值 //如果宽度是wrap_content我们设置为match_parent //否则:直接设为父控件值 setMeasuredDimension(parentWidth, (heightMode == MeasureSpec.EXACTLY) ? parentHeight: parentautoHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childLeft = 0; int childTop = 0; int index =0; int maxHeight = 0 ; for (int i = 0; i < getChildCount(); i++) { View childview = getChildAt(i); //如果该控件是gone的,则不作位置摆放 if (childview.getVisibility() == View.GONE) { continue; } // 得到子view的MarginLayoutParams,可以得到的margin等属性 MarginLayoutParams lp = (MarginLayoutParams) childview.getLayoutParams(); int childWidth = childview.getMeasuredWidth(); int childHeight = childview.getMeasuredHeight(); int left = childLeft + lp.leftMargin; if (index==0) { childTop = lp.topMargin; } maxHeight = getMaxTop(maxHeight,childHeight+lp.topMargin+lp.bottomMargin); if (left + childWidth + lp.rightMargin > parentWidth) { //对left的处理 childLeft = 0; left = lp.leftMargin; //对top的处理 index++; childTop += maxHeight; } //获取控件wrapContent时候的高度 parentautoHeight = childTop+maxHeight; childview.layout(left, childTop, left + childWidth, childTop + childHeight); childLeft += childWidth + lp.rightMargin + lp.leftMargin; } } //获取该行控件中最高的一个 private int getMaxTop(int oldTop,int newtop){ return Math.max(oldTop,newtop); } //要想使用子控件的margin //必须要实现这个方法,并且返回MarginLayoutParams @Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } }
引用
<com.example.king.kingcustomview.kingAutoLinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView style="@style/text" android:text="aaa" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="bbb" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="ccc" android:visibility="gone" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="ccc" style="@style/text" android:layout_width="100dp" android:layout_height="wrap_content"/> <TextView android:text="ccc" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="sssssssssssss" android:layout_marginLeft="10dp" android:paddingLeft="10dp" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_marginLeft="10dp" android:paddingLeft="10dp" android:text="aaa" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="cccccc" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="x" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="xx" style="@style/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </com.example.king.kingcustomview.kingAutoLinearLayout>
相关文章推荐
- 安卓自定义控件之自动换行ViewGroup
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- Android自定义控件系列六:自定义ViewGroup(一)实现ViewPager效果
- 不等宽子view自动换行的窗口(auto wrap viewgroup)
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- 自动换行的ViewGroup
- android自定义控件系列教程----继承ViewGroup实现带阻力效果的可回弹的SrollView
- 重写ViewGroup 实现组件自动换行 推荐
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- 用ViewGroup实现多View自动换行的功能
- 自定义ViewGroup实现水平布局空间不足自动换行的效果