google / flexbox-layout
- воскресенье, 8 мая 2016 г. в 03:11:19
Java
Flexbox for Android
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.1.1'
}
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);You can specify the following attributes for the FlexboxLayout:
flexDirection
flexWrap
justifyContent
alignItems
alignContent
Also you can specify the following attributes for the children of a FlexboxLayout.
layout_order
1 is set as a default value.layout_flexGrow
0 is set as a default value.layout_flexShrink
1 is set as a default value.layout_alignSelf
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
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.This library tries to achieve the same capabilities of the original Flexible 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
flex-flow is a shorthand for setting the flex-direction and flex-wrap properties,
specifying two attributes from a single attribute is not practical in Android.(2) There is no flex equivalent attribute
flex is a shorthand for setting the flex-grow, flex-shrink and flex-basis,
specifying those attributes from a single attribute is not practical.(3) layout_flexBasisPercent is introduced instead of
  flexBasis
layout_flexBasisPercent in this library and flex-basis property in the CSS are used to
determine the initial length of an individual flex item. The flex-basis property accepts width
values such as 1em, 10px, and content as strings as well as percentage values such as
10% and 30%, whereas layout_flexBasisPercent only accepts percentage values.
But specifying initial fixed width values can be done by specifying width (or height) values in
layout_width (or layout_height, varies depending on the flexDirection). Also, the same
effect can be done by specifying "wrap_content" in layout_width (or layout_height) if
developers want to achieve the same effect as 'content'. Thus, layout_flexBasisPercent only
accepts percentage values, which can't be done through layout_width (or layout_height) for
simplicity.(4) min-width and min-height can't be specified
Please read and follow the steps in CONTRIBUTING.md
Please see License