FlowLayout的使用

本文详细介绍了FlowLayout布局的使用方法,包括其基本属性设置、代码实现方式及实际应用效果。通过具体的代码示例展示了如何创建和使用FlowLayout来实现灵活的UI布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先给大家看效果图:


历史记录本地保存到share里面,在onStart里面获取调用数据,点击搜索的时候添加进share。

导入依赖:

compile 'com.nex3z:flow-layout:1.0.0'

https://github.com/nex3z/FlowLayout


Usage:

<com.nex3z.flowlayout.FlowLayout
    android:id="@+id/flow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:flChildSpacing="7dp"
    app:flRowSpacing="8dp" />

属性说明

这是github上的属性介绍,看不懂的可以略过,我后面有中文翻译,以及效果展示。


AttributeFormatDescription
flFlowbooleantrue to allow flow. false to restrict all child views in one row. The default is true.
flChildSpacingauto/dimensionThe horizontal spacing between child views. Either auto, or a fixed size. The default is 0dp.
flChildSpacingForLastRowauto/align/
dimension
The horizontal spacing between child views of the last row. Either auto, align or a fixed size. If not set, childSpacing will be used instead.
flRowSpacingauto/dimensionThe vertical spacing between rows. Either auto, or a fixed size. The default is 0dp.
flRtlbooleantrue to layout child views from right to left. false to layout from left to right. The default is false.
flMaxRowsintegerThe maximum height of FlowLayout in terms of number of rows.


中文:

FlowLayout flowLayout = (FlowLayout) findViewById(R.id.flow);
for (int i = 0; i < strList.size(); i++) {
    TextView textView = buildLabel(strList.get(i));
    flowLayout.addView(textView);
}
flowlayout就是一个布局,像这样添加textView进去,就初始化好了。


属性:

//默认为true,flase只显示一行

flowLayout.setFlow(false);
如图所示: 



//这个三个属性都是间距,横纵的距离

flowLayout.setChildSpacing(8);
flowLayout.setRowSpacing(8);
flowLayout.setChildSpacingForLastRow(8);

//这个属性默认是false,为true排列方向从右到左,上效果图。

app:flRtl="true"
这个属性我没有在代码里找到,只能在xml里面设置



//最大行数,如果设置为2那么显示出的行数只有两行。

flowLayout.setMaxRows(2);

如之前的图片,我本来有四行,设置以后只显示两行。 


我使用这个布局用到的代码如下:

for (int i = 0; i < strList.size(); i++) {
    TextView textView = buildLabel(strList.get(i));
    flowLayout.addView(textView);
}

private TextView buildLabel(final String text) {
    TextView textView = new TextView(this);
    textView.setText(text);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    textView.setPadding((int) dpToPx(16), (int) dpToPx(8), (int) dpToPx(16), (int) dpToPx(8));
    textView.setBackgroundResource(R.drawable.bg_gray);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            et.setText(text);
        }
    });
    return textView;
}

private float dpToPx(float dp) {
    return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}

需要的代码都在这里了。


此博客方便自己使用与他人交流,未经同意不允许他人转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值