dynamically add remove control form linearlayout

后端 未结 4 2027
一个人的身影
一个人的身影 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 11:45

    Remove view from parent on Android:

    View myView = findViewById(R.id.my_view);
    ViewGroup parent = (ViewGroup) myView.getParent();
    parent.removeView(myView);
    

    Android remove all child views:

    LinearLayout formLayout = (LinearLayout)findViewById(R.id.formLayout);
    formLayout.removeAllViews();
    

    Add view to parent on Android:

    Button myButton = new Button(getApplicationContext());
    myButton.setLayoutParameters(new LinearLayout.LayoutParams(
                                         LinearLayout.LayoutParams.FILL_PARENT,
                                         LinearLayout.LayoutParams.FILL_PARENT));
    
    myLayout.addView(myButton);
    

    you can use:

    LinearLayout.LayoutParams.FILL_PARENT
    

    or

    LinearLayout.LayoutParams.WRAP_CONTENT
    

提交回复
热议问题