{
    分享网正式开通,我们为大家提供免费资源,欢迎大家踊跃投稿!

安卓Flexbox的布局

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的属性:

  • flexDirection

    • 此属性确定主轴(和横轴,垂直于主轴)的方向。子项目放置在Flexbox布局内的方向。可能的值有:

      • 行(默认)
      • row_reverse
      • column_reverse

  • flexWrap

    • 此属性控制flex容器是单行还是多行,以及横轴的方向。可能的值有:

      • nowrap(默认)
      • wrap_reverse

  • justifyContent

    • 此属性控制沿主轴的对齐。可能的值有:

      • flex_start(默认)
      • flex_end
      • 中央
      • space_between
      • space_around

  • alignItems

    • 此属性控制沿横轴的对齐。可能的值有:

      • 拉伸(默认)
      • flex_start
      • flex_end
      • 中央
      • 基线

  • alignContent

    • 此属性控制flex容器中的flex线的对齐。可能的值有:

      • 拉伸(默认)
      • flex_start
      • flex_end
      • 中央
      • space_between
      • space_around

  • showDividerHorizontal(一个或多个none | beginning | middle | end

  • dividerDrawableHorizontal(参照绘)

    • 提出弯曲线(或当flexDirection设置为柔性项目之间的水平分隔线columncolumn_rebase)。
  • showDividerVertical(一个或多个none | beginning | middle | end

  • dividerDrawableVertical(参照绘)

    • 提出柔性物品(或当flexDirection设置为弯曲线之间的垂直分隔columncolumn_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 = 资源均来自第三方,谨慎下载,前往第三方网站下载


    米微资源分享网 , 版权所有丨本站资源仅限于学习研究,严禁从事商业或者非法活动!丨本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:安卓Flexbox的布局
    喜欢 ()分享 (0)