FlexboxLayout

常用效果:

横向显示自动换行:

xml文件:

 

         


动态添加控件:

   private void addView(View viewGroup, List strings, View.OnClickListener onClickListener) {
        FlexboxLayout flHotSearch = activity.bindId(viewGroup, R.id.fl_hot_search);
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).isEmpty()) continue;
            TextView textView = new TextView(viewGroup.getContext());
            textView.setText(strings.get(i));
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(getDP(16), getDP(0), getDP(16), getDP(0));
            textView.setOnClickListener(onClickListener);
            //构造宽高的属性
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, getDP(32));
            layoutParams.setMargins(getDP(0), getDP(8), getDP(8), 0);
            flHotSearch.addView(textView, layoutParams);
        }
    }

    private int getDP(int px) {
        return WindowUtils.dip2px(activity, px);
    }

 

你可能感兴趣的:(AndroidStudio)