Android中常见的流式布局的使用

public class Main2Activity extends AppCompatActivity {
    private TagFlowLayout tagFlowLayout;
    private EditText editText;
    private Button btn;
    private LayoutInflater inflater;
    private List list;
    private TextView tv;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            tagFlowLayout.setAdapter(new TagAdapter(list) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    tv = (TextView) inflater.inflate(R.layout.tv,tagFlowLayout,false);
                    tv.setText(s);
                    return tv;
                }
            });
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText = findViewById(R.id.edt);
        btn = findViewById(R.id.btn);
        tagFlowLayout = findViewById(R.id.id_flowlayout);
        list = new ArrayList<>();
        inflater = LayoutInflater.from(this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = editText.getText().toString().trim();
                list.add(trim);
                handler.sendEmptyMessageDelayed(1,0);
            }
        });
    }
}

布局



    




添加标签的文字背景 可写可不写



    
    
    


你可能感兴趣的:(Android中常见的流式布局的使用)