安卓Flexbox的布局

时间:2017-01-17 09:08 来源:互联网 作者:源码搜藏收藏

  • 源码类别:布局类Layouts
  • 源码大小:未知
  • 编码格式:gbk,utf8,不限
  • 运行环境:Android studio
  • 广告推荐

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);

支持的属性

FlexboxLayout的属性: