实现效果如图:

实现思路

重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)设置每个子View的大小

重写onLayout(boolean changed, int l, int t, int r, int b) 设置每个子View的位置

第一步:新建FlowLayout继承ViewGroup

packagecom.rong.activity;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.View;importandroid.view.ViewGroup;/***卡片布局**@author徐荣**/publicclassFlowLayoutextendsViewGroup{publicFlowLayout(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonLayout(booleanchanged,intl,intt,intr,intb){//当前子View的数量intchildSize=getChildCount();//获取行宽intlineWidth=getMeasuredWidth();//当前是第几行intlines=1;//当前累加的行宽intnowLineWidth=0;for(inti=0;i<childSize;i++){Viewview=getChildAt(i);//子View的宽度intchildWidth=view.getMeasuredWidth();//子View的高度intchildHeight=view.getMeasuredHeight();//如果当前的nowLineWidth+childWidth>=lineWidth则换行if(nowLineWidth+childWidth>=lineWidth){nowLineWidth=0;lines=lines+1;}//设置子View的位置view.layout(nowLineWidth,childHeight*(lines-1),nowLineWidth+childWidth,childHeight*lines);nowLineWidth=nowLineWidth+childWidth;//如果nowLineWidth>=lineWidth则换行if(nowLineWidth>=lineWidth){nowLineWidth=0;lines=lines+1;}}}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){super.onMeasure(widthMeasureSpec,heightMeasureSpec);//设置自己View的大小setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);for(inti=0;i<getChildCount();i++){Viewview=getChildAt(i);//设置每个子View的大小view.measure(view.getMeasuredWidth(),view.getMeasuredHeight());}}}

第二步:新建布局文件

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/black"android:orientation="vertical"><com.rong.activity.FlowLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Apple"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Cup"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Double"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Ear"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Flower"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Game"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hotdog"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="interseting"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="joker"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="king"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="mother"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="lost"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="noting"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="orange"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="poker"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="qustion"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ring"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="string"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="type"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="unit"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="vertion"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="west"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="x"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="young"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="zip"/></com.rong.activity.FlowLayout></RelativeLayout>

Run

更多相关文章

  1. Android(安卓)控件之ImageSwitcher图片切换器
  2. android中跳转系统自带界面
  3. Textview与Button设置圆角
  4. 如何将图片压缩至固定大小以下
  5. Android(安卓)build/envsetup.sh分析
  6. 解决scrollview中内容改变后自动滑动到底部的问题
  7. android系统自带的intent集锦
  8. 安卓 Android之开发简单小应用(三)
  9. Android(SpreadTurm)开锁前设置一个亮度,解锁后再设置一个亮度

随机推荐

  1. Android中通过其他APP启动Activity的四种
  2. 史上最全的android开发资料大集合
  3. Android多渠道打包修改资源文件
  4. Android(安卓)Design Support Library使
  5. android网格连接
  6. Android(安卓)7.1 设置不支持遥控操作?
  7. 安卓按钮有按下去的效果的实现方法
  8. Android中Media Framework浅析(二)——Medi
  9. Android之如何解决右上角不显示3个点的菜
  10. Android开发之EditText属性详解