美文网首页
FlexboxLayout的基本用法

FlexboxLayout的基本用法

作者: 好学人 | 来源:发表于2019-04-20 15:18 被阅读0次

项目地址

官方文档:https://github.com/google/flexbox-layout

添加依赖

  • 已迁移到androidx的项目请使用1.1.0或更高版本:
dependencies {
    implementation 'com.google.android:flexbox:1.1.0'
}
  • 未迁移到androidx的项目请使用1.0.0版本:
dependencies {
    implementation 'com.google.android:flexbox:1.0.0'
}

基本用法

  1. FlexboxLayout继承ViewGroup,可以直接在xml布局中使用,也可以在代码中动态添加View:flexboxLayout.addView(itemView);
<com.google.android.flexbox.FlexboxLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:flexWrap="wrap">

    <TextView
        android:layout_width="120dp"
        android:layout_height="80dp"
        app:layout_flexBasisPercent="50%"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:layout_alignSelf="center" />

    <TextView
        android:layout_width="160dp"
        android:layout_height="80dp"
        app:layout_alignSelf="flex_end" />
</com.google.android.flexbox.FlexboxLayout>
  1. FlexboxLayout可以与RecyclerView结合使用,只需要使用FlexboxLayoutManager替换RecyclerViewLayoutManager即可:
RecyclerView recyclerView = layout.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.ROW);
recyclerView.setLayoutManager(layoutManager);

相关文章

网友评论

      本文标题:FlexboxLayout的基本用法

      本文链接:https://www.haomeiwen.com/subject/uuqlgqtx.html