FlexboxLayout的基本用法

2019-04-20  本文已影响0人  好学人

项目地址

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

添加依赖

dependencies {
    implementation 'com.google.android:flexbox:1.1.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);
上一篇 下一篇

猜你喜欢

热点阅读