FlowLayout的使用

先给大家看效果图:


历史记录本地保存到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上的属性介绍,看不懂的可以略过,我后面有中文翻译,以及效果展示。


Attribute Format Description
flFlow boolean true to allow flow. false to restrict all child views in one row. The default is true.
flChildSpacing auto/dimension The horizontal spacing between child views. Either auto, or a fixed size. The default is 0dp.
flChildSpacingForLastRow auto/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.
flRowSpacing auto/dimension The vertical spacing between rows. Either auto, or a fixed size. The default is 0dp.
flRtl boolean true to layout child views from right to left. false to layout from left to right. The default is false.
flMaxRows integer The 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());
}

需要的代码都在这里了。


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章