作者:金燁欣_973 | 来源:互联网 | 2023-02-01 22:40
公司的项目需要使用流式布局显示选项,在网上看了不少的类似的东西,找了好久,发现了一个比较好用方式。首先导入依赖:compile‘com.nex3z:flow-layout:1.0.
公司的项目需要使用流式布局显示选项,在网上看了不少的类似的东西,找了好久,发现了一个比较好用方式。
首先导入依赖:
compile ‘com.nex3z:flow-layout:1.0.0’ 在Github上开源:点击查看
布局代码:
android:id="@+id/flow" android:layout_width=“match_parent” android:layout_height=“wrap_content” app:flChildSpacing=“7dp” app:flRowSpacing=“8dp” /> 属性说明(中文版):
FlowLayout flowLayout = (FlowLayout) findViewById(R.id.flow); for (int i = 0; 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 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()); } 效果还是挺好用的,颜色自己设置。特性就是可以自动根据标签文字的长度自动换行。 APP开发、一元建站、货运APP、冷练车APP、直播系统、小程序开发,找上海捌跃网络科技有限公司