当前位置:网站首页>Dynamically add and delete layouts

Dynamically add and delete layouts

2022-04-23 18:41:00 Xiao Xiaofeng

Main code :

   /** *  Add and remove people  * @param str */
    private void add(String str) {
     ll = (LinearLayout) findViewById(R.id.line_);
        params = new LinearLayout.LayoutParams(AutoLinearLayout.LayoutParams.MATCH_PARENT, 120);
        View l = LayoutInflater.from(this).inflate(R.layout.item_, null);
        params.setMargins(20, 20, 20, 20);//4 The two parameters are to set the upper left and lower right margins in order 
        if (l.getParent() != null)
            ((ViewGroup) l.getParent()).removeView(l);
        ll.addView(l, params);
        TextView tv = l.findViewById(R.id.tv_);
        tv.setText(str);

        l.findViewById(R.id.img_).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ll.removeView((View) view.getParent());// Remove the currently clicked add item 

            }
        });
    }

版权声明
本文为[Xiao Xiaofeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210604091508.html