dynamically add remove control form linearlayout

后端 未结 4 2025
一个人的身影
一个人的身影 2021-01-05 11:15

I have 3 layouts, I need when click on button access certain layout and ad remove controls from and in it any idea how to achieve that , this is the code I use



        
4条回答
  •  一生所求
    2021-01-05 12:03

    Sample for dynamically add or remove a view:

    TextView tv = new TextView(this);
    
            tv.setWidth(LayoutParams.WRAP_CONTENT);
    
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.RIGHT;
            tv.setLayoutParams(params);
            tv.setTextAppearance(this, R.style.darkTextNormal);
            tv.setBackgroundResource(R.color.lightBlue);
            tv.setTextSize(16);
    
    yourLinearLayout.addView(tv);
    

    // or

    yourLinearLayout.removeView(tv);
    

提交回复
热议问题