two different layouts for one activity

后端 未结 5 1273
春和景丽
春和景丽 2020-12-08 15:57

Is it possible to have two different layouts for different cases in the same activity or do I have to use intent to call another activity with a di

5条回答
  •  囚心锁ツ
    2020-12-08 16:12

    There are a number of ways to go about this. The other answers include at least two approaches - using setContentView depending on the case and using fragments. There is one more I'd like to talk about. Say for instance, you include two layouts

    
    
    
    

    In your java code, you can then hide or show your layouts depending on the use case. For instance, setting the content view to show the layout above shows layout1. When user clicks next button, you can then get a reference to layout1 and set it's visibility to gone and layout2's visibility to visible.

    LinearLayout layout1 = findViewById(R.id.layout1);
    LinearLayout layout2 = findViewById(R.id.layout2);
    
    buttonNext.setOnClickListener(new View.OnClickListener()
    { 
    layout1.setVisibility(View.GONE);
    layout2.setVisibility(View.VISIBLE);
    });
    

提交回复
热议问题