代码已经整理好,这里要说一下,因为手动和Json获取写法不一样。

手动直接xml设置:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#E1E6F6"android:orientation="vertical"><com.zhy.zhy_flowlayout02.FlowLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewstyle="@style/text_flag_01"android:text="Welcome"/><TextViewstyle="@style/text_flag_01"android:text="IT工程师"/><TextViewstyle="@style/text_flag_01"android:text="学习ing"/><TextViewstyle="@style/text_flag_01"android:text="恋爱ing"/><TextViewstyle="@style/text_flag_01"android:text="挣钱ing"/><TextViewstyle="@style/text_flag_01"android:text="努力ing"/><TextViewstyle="@style/text_flag_01"android:text="Ithickican"/></com.zhy.zhy_flowlayout02.FlowLayout><com.zhy.zhy_flowlayout02.FlowLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="Welcome"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="IT工程师"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="学习ing"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="恋爱ing"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="挣钱ing"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="努力ing"android:textColor="#888888"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_02"android:text="Ithickican"android:textColor="#888888"/></com.zhy.zhy_flowlayout02.FlowLayout><com.zhy.zhy_flowlayout02.FlowLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="Welcome"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="IT工程师"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="学习ing"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="恋爱ing"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="挣钱ing"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="努力ing"android:textColor="#43BBE7"/><TextViewstyle="@style/text_flag_01"android:background="@drawable/flag_03"android:text="Ithickican"android:textColor="#43BBE7"/></com.zhy.zhy_flowlayout02.FlowLayout></LinearLayout>

效果如下:

Json获取:

if(message.what==1){//处理操作pingjia=newArrayList<Pingjia>();pingjia.addAll(JsonUtils.getBeanList(message.result,"Items",Pingjia.class));FlowLayoutlayout=(FlowLayout)findViewById(R.id.layou23);LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);params.gravity=Gravity.CENTER_VERTICAL;//加载TextViewfor(inti=0;i<pingjia.size();i++){Pingjiaping=pingjia.get(i);tvNav=newTextView(this);tvNav.setTextSize(13.f);//tvNav.setWidth((int)Math.floor(Double.valueOf(a)));//tvNav.setHeight((int)Math.floor(Double.valueOf(b)));tvNav.setText(ping.getContent());tvNav.setId(ping.getId());tvNav.setBackgroundResource(R.anim.flag_022);tvNav.setTag(false);tvNav.setTextColor(getResources().getColor(R.color.gray));//tvNav.setPadding(30,10,30,10);//tvNav.setGravity(Gravity.CENTER_HORIZONTAL);tvNav.setOnClickListener(newTvClickListener(tvNav));layout.addView(tvNav,params);}privateTextViewtvNav;privateStringstr="";privateStringstrId="";/***标题栏textview点击事件**@authorfei**/privatefinalclassTvClickListenerimplementsView.OnClickListener{//privateintindex;privateTextViewtvNav;publicTvClickListener(TextViewtvNav){super();//this.index=index;this.tvNav=tvNav;}@OverridepublicvoidonClick(Viewv){Booleanisclick=(Boolean)tvNav.getTag();if(!isclick){tvNav.setBackgroundResource(R.anim.flag_0444);tvNav.setTextColor(getResources().getColor(R.color.red));tvNav.setTag(true);//显示到上面str=str+tvNav.getText().toString()+",";ed_en.setText(str);//记录IdstrId=strId+tvNav.getId()+",";//strId=strId.substring(0,strId.length()-1);//MyLogUtil.v("传->strId",strId);}else{tvNav.setBackgroundResource(R.anim.flag_022);tvNav.setTextColor(getResources().getColor(R.color.gray));tvNav.setTag(false);//显示到上面str=str.replace(tvNav.getText().toString()+",","");ed_en.setText(str);//记录IdstrId=strId.replace(tvNav.getId()+",","");//strId=strId.substring(0,strId.length()-1);//MyLogUtil.v("Delete->strId",strId);}//ToastUtil.showToastLong(tvNav.getId()+","//+tvNav.getText().toString());}}

xml设置:

<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="@dimen/dimens_10"android:layout_marginLeft="@dimen/x10"android:layout_marginRight="@dimen/x10"android:layout_marginTop="@dimen/y20"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:drawableLeft="@drawable/biaoqian1"android:drawablePadding="@dimen/dimens_10"android:text="@string/bq3"android:textColor="@color/black"android:textSize="15sp"/><com.kangxin.patient.utils.FlowLayoutandroid:id="@+id/layou23"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/y10"android:orientation="vertical"></com.kangxin.patient.utils.FlowLayout></LinearLayout>

效果如下:

最后是自定义layout类FlowLayout:

packagecom.zhy.zhy_flowlayout02;importjava.util.ArrayList;importjava.util.List;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.View;importandroid.view.ViewGroup;publicclassFlowLayoutextendsViewGroup{privatestaticfinalStringTAG="FlowLayout";publicFlowLayout(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedViewGroup.LayoutParamsgenerateLayoutParams(ViewGroup.LayoutParamsp){returnnewMarginLayoutParams(p);}@OverridepublicViewGroup.LayoutParamsgenerateLayoutParams(AttributeSetattrs){returnnewMarginLayoutParams(getContext(),attrs);}@OverrideprotectedViewGroup.LayoutParamsgenerateDefaultLayoutParams(){returnnewMarginLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);}/***负责设置子控件的测量模式和大小根据所有子控件设置自己的宽和高*/@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){super.onMeasure(widthMeasureSpec,heightMeasureSpec);//获得它的父容器为它设置的测量模式和大小intsizeWidth=MeasureSpec.getSize(widthMeasureSpec);intsizeHeight=MeasureSpec.getSize(heightMeasureSpec);intmodeWidth=MeasureSpec.getMode(widthMeasureSpec);intmodeHeight=MeasureSpec.getMode(heightMeasureSpec);Log.e(TAG,sizeWidth+","+sizeHeight);//如果是warp_content情况下,记录宽和高intwidth=0;intheight=0;/***记录每一行的宽度,width不断取最大宽度*/intlineWidth=0;/***每一行的高度,累加至height*/intlineHeight=0;intcCount=getChildCount();//遍历每个子元素for(inti=0;i<cCount;i++){Viewchild=getChildAt(i);//测量每一个child的宽和高measureChild(child,widthMeasureSpec,heightMeasureSpec);//得到child的lpMarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();//当前子空间实际占据的宽度intchildWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;//当前子空间实际占据的高度intchildHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;/***如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height然后开启新行*/if(lineWidth+childWidth>sizeWidth){width=Math.max(lineWidth,childWidth);//取最大的lineWidth=childWidth;//重新开启新行,开始记录//叠加当前高度,height+=lineHeight;//开启记录下一行的高度lineHeight=childHeight;}else//否则累加值lineWidth,lineHeight取最大高度{lineWidth+=childWidth;lineHeight=Math.max(lineHeight,childHeight);}//如果是最后一个,则将当前记录的最大宽度和当前lineWidth做比较if(i==cCount-1){width=Math.max(width,lineWidth);height+=lineHeight;}}setMeasuredDimension((modeWidth==MeasureSpec.EXACTLY)?sizeWidth:width,(modeHeight==MeasureSpec.EXACTLY)?sizeHeight:height);}/***存储所有的View,按行记录*/privateList<List<View>>mAllViews=newArrayList<List<View>>();/***记录每一行的最大高度*/privateList<Integer>mLineHeight=newArrayList<Integer>();@OverrideprotectedvoidonLayout(booleanchanged,intl,intt,intr,intb){mAllViews.clear();mLineHeight.clear();intwidth=getWidth();intlineWidth=0;intlineHeight=0;//存储每一行所有的childViewList<View>lineViews=newArrayList<View>();intcCount=getChildCount();//遍历所有的孩子for(inti=0;i<cCount;i++){Viewchild=getChildAt(i);MarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();intchildWidth=child.getMeasuredWidth();intchildHeight=child.getMeasuredHeight();//如果已经需要换行if(childWidth+lp.leftMargin+lp.rightMargin+lineWidth>width){//记录这一行所有的View以及最大高度mLineHeight.add(lineHeight);//将当前行的childView保存,然后开启新的ArrayList保存下一行的childViewmAllViews.add(lineViews);lineWidth=0;//重置行宽lineViews=newArrayList<View>();}/***如果不需要换行,则累加*/lineWidth+=childWidth+lp.leftMargin+lp.rightMargin;lineHeight=Math.max(lineHeight,childHeight+lp.topMargin+lp.bottomMargin);lineViews.add(child);}//记录最后一行mLineHeight.add(lineHeight);mAllViews.add(lineViews);intleft=0;inttop=0;//得到总行数intlineNums=mAllViews.size();for(inti=0;i<lineNums;i++){//每一行的所有的viewslineViews=mAllViews.get(i);//当前行的最大高度lineHeight=mLineHeight.get(i);Log.e(TAG,"第"+i+"行:"+lineViews.size()+","+lineViews);Log.e(TAG,"第"+i+"行,:"+lineHeight);//遍历当前行所有的Viewfor(intj=0;j<lineViews.size();j++){Viewchild=lineViews.get(j);if(child.getVisibility()==View.GONE){continue;}MarginLayoutParamslp=(MarginLayoutParams)child.getLayoutParams();//计算childView的left,top,right,bottomintlc=left+lp.leftMargin;inttc=top+lp.topMargin;intrc=lc+child.getMeasuredWidth();intbc=tc+child.getMeasuredHeight();Log.e(TAG,child+",l="+lc+",t="+t+",r="+rc+",b="+bc);child.layout(lc,tc,rc,bc);left+=child.getMeasuredWidth()+lp.rightMargin+lp.leftMargin;}left=0;top+=lineHeight;}}}

最后感谢hongyang大神~Loader大神~

更多相关文章

  1. Android软键盘适配问题
  2. Android(安卓)EditText 设置行距不影响光标高度
  3. Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
  4. android listview实现表格样式
  5. 怎么去掉联系人、通话记录、拨号列表界面中的电话号码中间的空格
  6. Android(安卓)图片压缩的方法大全
  7. Android(安卓)TextView中android:textIsSelectable属性在Recycle
  8. android 通话记录次数
  9. scrollView的fillviewport

随机推荐

  1. 一个使用FFmpeg库读取3gp视频的例子-Andr
  2. Android 智能TV电视系统遥控器键值添加
  3. android滑动解锁
  4. android之Activity基本跳转
  5. 【Android(安卓)开发】 : Activity之间传
  6. Android学习心得
  7. MapMe
  8. Android中打开多个Activity,返回到第一个A
  9. 学习Android之用户界面基础
  10. Unity编译Android的原理解析和apk打包分