一、概述:
(一)、什么是FlowLayout?
何为FlowLayout,
就是控件根据ViewGroup的宽,自动的往右添加,如果当前行剩余空间不足,则自动添加到下一行。有点所有的控件都往左飘的感觉,第一行满了,往第二行飘,所以也叫流式布局。
Android并没有提供流式布局,但是某些场合中,流式布局还是非常适合使用的,比如关键字标签,搜索热词列表等,如下图:
这些都特别适合使用FlowLayout,
github已经有了这样FlowLayout,
当然使用我们自己定义的FlowLayout实现上面的标签效果,就不仅仅是学会使用一个控件,而是学会写一个控件。
二、制作步骤:
(一)、分析
1、对于FlowLayout,需要指定的LayoutParams,我们目前只需要能够识别margin即可,即使用MarginLayoutParams.
2、onMeasure中计算所有childView的宽和高,然后根据childView的宽和高,计算自己的宽和高。(当然,如果不是wrap_content,直接使用父ViewGroup传入的计算值即可)
3、onLayout中对所有的childView进行布局。
(二)、
generateLayoutParams
因为我们只需要支持margin,所以直接使用系统的MarginLayoutParams
return new MarginLayoutParams(getContext(), attrs);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
res/drawble/flog_02.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<corners android:radius="40dp"/>
<stroke android:color="#C9C9C9" android:width="2dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
flag_03.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<corners android:radius="40dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
布局文件:
[xml]
<LinearLayout xmlns: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.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/text_flag_01"
android:text="Welcome" />
<TextView
style="@style/text_flag_01"
android:text="IT工程师" />
<TextView
style="@style/text_flag_01"
android:text="学习ing" />
<TextView
style="@style/text_flag_01"
android:text="恋爱ing" />
<TextView
style="@style/text_flag_01"
android:text="挣钱ing" />
<TextView
style="@style/text_flag_01"
android:text="努力ing" />
<TextView
style="@style/text_flag_01"
android:text="I thick i can" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="Welcome"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="IT工程师"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="学习ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="恋爱ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="挣钱ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="努力ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="I thick i can"
android:textColor="#888888" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="Welcome"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="IT工程师"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="学习ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="恋爱ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="挣钱ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="努力ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="I thick i can"
android:textColor="#43BBE7" />
</com.zhy.zhy_flowlayout02.FlowLayout>
</LinearLayout>
效果图:
[xml]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<corners android:radius="40dp"/>
<stroke android:color="#C9C9C9" android:width="2dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
flag_03.xml
[xml]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<corners android:radius="40dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
布局文件:
[xml]
<LinearLayout xmlns: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.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/text_flag_01"
android:text="Welcome" />
<TextView
style="@style/text_flag_01"
android:text="IT工程师" />
<TextView
style="@style/text_flag_01"
android:text="学习ing" />
<TextView
style="@style/text_flag_01"
android:text="恋爱ing" />
<TextView
style="@style/text_flag_01"
android:text="挣钱ing" />
<TextView
style="@style/text_flag_01"
android:text="努力ing" />
<TextView
style="@style/text_flag_01"
android:text="I thick i can" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="Welcome"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="IT工程师"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="学习ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="恋爱ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="挣钱ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="努力ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="I thick i can"
android:textColor="#888888" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="Welcome"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="IT工程师"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="学习ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="恋爱ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="挣钱ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="努力ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="I thick i can"
android:textColor="#43BBE7" />
</com.zhy.zhy_flowlayout02.FlowLayout>
</LinearLayout>
效果图: