How can i change included xml layout to another layout on java code?

前端 未结 3 1079
灰色年华
灰色年华 2020-12-13 01:20

i have a problem. I develop android application.

I included second layout to first layout like this:

         


        
3条回答
  •  無奈伤痛
    2020-12-13 01:49

    There is two ways to change layouts in code:

    A. Change visibility. Include tow different layouts in your xml:

    
    
    

    and in code use this:

    findViewById(R.id.id1).setVisibility(View.GONE);
    findViewById(R.id.id2).setVisibility(View.VISIBLE);
    

    B. Manually add children. Use the same xml as you use now and add in code you can add or delete children:

    yourRelativeLayout.removeAllViews();
    yourRelativeLayout.addView(viewToInclude);
    

    Offtopic:

    You don't need to write xmlns:android param in RelativeLayout. Just for the most top tag in layout file.

提交回复
热议问题