//依赖
implementation 'com.hyman:flowlayout-lib:1.1.2'
//
View inflate = inflater.inflate(R.layout.fragment_twok, container, false); flow = inflate.findViewById(R.id.flow); for (int i = 0; i <10; i++) { list.add("Android"); list.add("Java"); list.add("IOS"); list.add("python"); } flow.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return false; } }); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(10, 5, 10, 5); if (flow != null) { flow.removeAllViews(); } for (int i = 0; i < list.size(); i++) { TextView tv = new TextView(getActivity()); tv.setPadding(28, 10, 28, 10); tv.setText(list.get(i)); tv.setMaxEms(10); tv.setSingleLine(); tv.setLayoutParams(layoutParams); flow.addView(tv, layoutParams); } return inflate; }
//布局
<com.zhy.view.flowlayout.FlowLayout android:id="@+id/flow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#e8e8e8" android:padding="5dp" app:max_select="1" />