Android 流式布局实现方式

Android 流式布局实现方式

1、FlexboxLayout

FlexboxLayout是Google开源的一个强大的控件,直接继承ViewGroup。

dependencies {
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
}

var flContent = findViewById(R.id.fl_content)
        flContent.flexWrap = FlexWrap.WRAP
        flContent.flexDirection = FlexDirection.ROW
        map.keys?.forEach {
            flContent.layoutInflate(R.layout.item_filter).apply {
                var tvName = this.findViewById(R.id.tv_name)
                tvName.text = it
                tag = false
  
                flContent.addView(this)
            }
        }

2、Recyclerview+FlexboxLayoutManager

implementation 'com.google.android:flexbox:3.0.0'//Android官方流式布局
FlexboxLayoutManager layoutManager=new FlexboxLayoutManager(this);
layoutManager.setFlexDirection(FlexDirection.ROW);
//layoutManager.setJustifyContent(JustifyContent.CENTER);
//layoutManager.setAlignItems(AlignItems.CENTER);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(adapter);

3、ConstraintLayout + Flow

Constraintlayout 2.0 新增了Flow流式虚拟布局。

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  
for (i in 0..4) {
    val customView = CustomComponent (this)
    customView.id = generateViewId()
    constraintLayout.addView(customView,i)
    flow.addView(customView)
}

你可能感兴趣的:(代码块,android,流式布局,Flow,FlexboxLayout)