FlowLayout控件使用效果

 private FlowLayout flow;
    private List strings;
    private EditText edit_title;
    private String text_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit_title = findViewById(R.id.edit_text);
        text_title = edit_title.getText().toString();
        flow = findViewById(R.id.flow);
        strings = new ArrayList<>();
        strings.add("小米3是一代机王");
        strings.add("华为");
        strings.add("华为5g");
        strings.add("华为");
        strings.add("百度ai");
        strings.add("小米ai是产业链的一个布局产品");
        strings.add("虚拟于现实");
        strings.add("小米");
        strings.add(text_title);
        //往容器内添加TextView数据
        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 < strings.size(); i++) {
            TextView tv = new TextView(this);
            tv.setPadding(28, 10, 28, 10);
            tv.setText(strings.get(i));
            tv.setMaxEms(10);
            tv.setSingleLine();
            //tv.setBackgroundResource(R.drawable.ic_search_black_24dp);
            tv.setLayoutParams(layoutParams);
            flow.addView(tv, layoutParams);
        }
    }

 

你可能感兴趣的:(FlowLayout控件使用效果)