Android FlowLayout is an enhanced LinearLayout, in which child elements will automatically flow when there is not enough space in current line.
Android FlowLayout gives you more efficiency when you want to develop some layout pattern. (see "Demonstration" for details)
child will be place in specific orientation , horizontal or vertical when there is no enough space for more child , it will be placed in another line
child with parameter "weight" will share the rest of space in current line
notes:
when you use "weight" ,please ensure that "width"/"height" is zero only children in same line can share rest of space on current line
child can be placed in different gravity. for example when orientation is horizontal , children can be placed from left or right edge flow layout
notes:
"gravity=left" in horizontal mode equals "gravity=top" in horizontal mode "gravity=right" in horizontal mode equals "gravity=bottom" in horizontal mode
This is amazing! When you want to create layout like this:
Old school way, using LinearLayout:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="xxdip">
<View
android:id="@+id/fist_child_in_first_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
<View
android:id="@+id/second_child_in_first_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="xxdip">
<View
android:id="@+id/fist_child_in_second_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
<View
android:id="@+id/second_child_in_second_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
</LinearLayout>
</LinearLayout>
Using FlowLayout(much easier!):
<com.suan.flowlayout.FlowLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
flow:lineNum="1"
flow:weight="1"
android:id="@+id/fist_child_in_first_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
<View
flow:lineNum="1"
flow:weight="1"
android:id="@+id/second_child_in_first_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
<View
flow:lineNum="2"
flow:weight="1"
android:id="@+id/fist_child_in_second_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
<View
flow:lineNum="2"
flow:weight="1"
android:id="@+id/second_child_in_second_line"
android:layout_width="0dp"
android:layout_height="xxdip"/>
</com.suan.flowlayout.FlowLayout>
Sample usage:
-
Include this repo as module in your project
-
Use layout like this
<com.suan.flowlayout.FlowLayout flow:orientation="horizontal" flow:gravity="left" flow:horizontalSpacing="10dp" flow:verticalSpacing="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button .../> <... .../> </com.suan.flowlayout.FlowLayout>
If you want to use custom attribute of FlowLayout, you should define the namespace first:
xmlns:f="http://schemas.android.com/apk/res/your.namespace"
or
xmlns:flow="http://schemas.android.com/apk/res-auto"
then you can use custom attribute below
specific child orientation for this layout "horizontal" or "vertical" default value is "horizontal"
specific child gravity for this layout "left"/"top" or "right"/"bottom" default value is "left"/"top"
specific child spacing for this layout , spacing between child horizontally default value is "0"
specific child spacing for this layout , spacing between child vertically default value is "0"
set weight for this child ,and child with attribute "weight" in same line will share the rest of space according to weight
same function as "horizontalSpacing" ,but this will only has effect on this child
same function as "verticalSpacing" ,but this will only has effect on this child
specific line number for this child
notes: same line number does't always means same line or row ,only if there are coherent and space is enough
Copyright 2014, suanmiao
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.