FlexboxLayout是库项目带来的类似功能的 CSS弹性框布局模块到Android。
添加下面的依赖关系到你的build.gradle
文件中:
dependencies {
compile 'com.google.android:flexbox:0.2.5'
}
FlexboxLayout扩展了ViewGroup,如LinearLayout和RelativeLayout。您可以从布局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 : 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>
或从代码如:
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
此属性确定主轴(和横轴,垂直于主轴)的方向。子项目放置在Flexbox布局内的方向。可能的值有:
flexWrap
此属性控制flex容器是单行还是多行,以及横轴的方向。可能的值有:
justifyContent
此属性控制沿主轴的对齐。可能的值有:
alignItems
此属性控制沿横轴的对齐。可能的值有:
alignContent
此属性控制flex容器中的flex线的对齐。可能的值有:
showDividerHorizontal(一个或多个none | beginning | middle | end
)
dividerDrawableHorizontal(参照绘)
column
或column_rebase
)。
showDividerVertical(一个或多个none | beginning | middle | end
)
dividerDrawableVertical(参照绘)
column
或column_rebase
)。
showDivider(一个或多个none | beginning | middle | end
)
dividerDrawable(参照绘)
justifyContent="space_around"
或alignContent="space_between"
...等),用于将弯曲线或柔性项目之间的空间,你可能会看到意想不到的空间。请避免同时使用这些。放置垂直和水平分隔线的示例。
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 =
安卓Flexbox的布局转载请注明出处http://www.codesocang.com/kj/layouts/34330.html
源码搜藏网所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与源码搜藏网无关
标签:
下一篇:没有了