首先要导依赖:compile 'com.fynn.fluidlayout:fluidlayout:1.0'
<com.fynn.fluidlayout.FluidLayout
android:id="@+id/fluid"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="15dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"></com.fynn.fluidlayout.FluidLayout>
在主方法代码如下:
String[] arrs={"铝合金门窗","罗汉果茶","容声冰箱","小米音响","魔术头巾","哑铃","吸尘器干湿两用"}; private FluidLayout fluid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sousuo); fluid = findViewById(R.id.fluid); for (int x = 0; x < arrs.length; x++) { Button tv = new Button(this); tv.setText(arrs[x]); tv.setTextSize(16); FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(3, 3, 3, 3); fluid.addView(tv, params); } }效果图: