| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.
Add the following dependency to your build.gradle file:
dependencies {
compile 'com.google.android:flexbox:0.2.3'
}
FlexboxLayout extends the ViewGroup like LinearLayout and RelativeLayout. You can specify the attributes from a layout XML like:
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="@+id/textview1"
android:layout_width="120dp"
android:layout_height="80dp"
app:layout_flexBasisPercent="50%"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_alignSelf="center"
/>
<TextView
android:id="@+id/textview3"
android:layout_width="160dp"
android:layout_height="80dp"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>Or from code like:
FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout);
flexboxLayout.setFlexDirection(FlexboxLayout.FLEX_DIRECTION_COLUMN);
View view = flexboxLayout.getChildAt(0);
FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams();
lp.order = -1;
lp.flexGrow = 2;
view.setLayoutParams(lp);flexDirection
flexWrap
justifyContent
alignItems
alignContent
showDividerHorizontal (one or more of none | beginning | middle | end)
dividerDrawableHorizontal (reference to a drawable)
showDividerVertical (one or more of none | beginning | middle | end)
dividerDrawableVertical (reference to a drawable)
showDivider (one or more of none | beginning | middle | end)
dividerDrawable (reference to a drawable)
Example of putting both vertical and horizontal dividers.
res/drawable/divider.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="8dp"
android:height="12dp" />
<solid android:color="#44A444" />
</shape> res/layout/content_main.xml
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:alignContent="flex_start"
app:alignItems="flex_start"
app:flexWrap="wrap"
app:showDivider="beginning|middle"
app:dividerDrawable="@drawable/divider" >
<TextView
style="@style/FlexItem"
android:layout_width="220dp"
android:layout_height="80dp"
android:text="1" />
<TextView
style="@style/FlexItem"
android:layout_width="120dp"
android:layout_height="80dp"
android:text="2" />
<TextView
style="@style/FlexItem"
android:layout_width="160dp"
android:layout_height="80dp"
android:text="3" />
<TextView
style="@style/FlexItem"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="4" />
<TextView
style="@style/FlexItem"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="5" />layout_order (integer)
layout_flexGrow (float)
This attribute determines how much this child will grow if positive free space is distributed relative to the rest of other flex items included in the same flex line. If a flex item has a positive layout_flexGrow value, the item will take up the remaining space in the flex line. If multiple flex items in the same flex line have positive layout_flexGrow values, the remaining free space is distributed depending on the proportion of their declared layout_flexGrow value. (Similar to the layout_weight attribute in the LinearLayout) If not specified, 0 is set as a default value.
layout_flexShrink (float)
layout_alignSelf
This attribute determines the alignment along the cross axis (perpendicular to the main axis). The alignment in the same direction can be determined by the alignItems in the parent, but if this is set to other than auto, the cross axis alignment is overridden for this child. Possible values are:
layout_flexBasisPercent (fraction)
The initial flex item length in a fraction format relative to its parent. The initial main size of this child view is trying to be expanded as the specified fraction against the parent main size. If this value is set, the length specified from layout_width (or layout_height) is overridden by the calculated value from this attribute. This attribute is only effective when the parent's length is definite (MeasureSpec mode is MeasureSpec.EXACTLY). The default value is -1, which means not set.
layout_minWidth / layout_minHeight (dimension)
These attributes impose minimum size constraints for the children of FlexboxLayout. A child view won't be shrank less than the value of these attributes (varies based on the flexDirection attribute as to which attribute imposes the size constraint along the main axis) regardless of the layout_flexShrink attribute.
layout_maxWidth / layout_maxHeight (dimension)
These attributes impose maximum size constraints for the children of FlexboxLayout. A child view won't be expanded more than the value of these attributes (varies based on the flexDirection attribute as to which attribute imposes the size constraint along the main axis) regardless of the layout_flexGrow attribute.
layout_wrapBefore (boolean)
This attribute forces a flex line wrapping, the default value is false. i.e. if this is set to true for a flex item, the item will become the first item of a flex line. (A wrapping happens regardless of the flex items being processed in the the previous flex line) This attribute is ignored if the flex_wrap attribute is set to nowrap. The equivalent attribute isn't defined in the original CSS Flexible Box Module specification, but having this attribute is useful for Android developers. For example, to flatten the layouts when building a grid like layout or for a situation where developers want to put a new flex line to make a semantic difference from the previous one, etc.
This library tries to achieve the same capabilities of the original Flexible Box specification as much as possible, but due to some reasons such as the way specifying attributes can't be the same between CSS and Android XML, there are some known differences from the original specification.
(1) There is no flex-flow equivalent attribute
(2) There is no flex equivalent attribute
(3) layout_flexBasisPercent is introduced instead of flexBasis
(4) layout_wrapBefore is introduced.
The app module works as a playground demo app for trying various values for the supported attributes. You can install it by
./gradlew installDebug
Please read and follow the steps in CONTRIBUTING.md
Please see LICENSE
| Back | FazBrowse Home | New Git URL |