Android: is using setContentView multiple times bad while changing layouts?

后端 未结 3 1096
青春惊慌失措
青春惊慌失措 2020-11-28 15:34

is using setContentView multiple times bad while changing layouts?

Some people say that it\'s bad and they never say why.

and is there some other thing to ch

3条回答
  •  醉话见心
    2020-11-28 15:54

    Yes this is bad, because it inflates your activity with your layout, and if your layout has a lot of views, it may take time.

    To avoid that you should use a ViewAnimator, where you put all your layouts and you switch by showNext() and showPrevious(), i.e:

    
    
          
    
          
    
    
    

    And in your code:

    // Don't forget the setContentView
    //
    // Load the ViewAnimator and display the first layout
    ViewAnimator va = (ViewAnimator) findViewById(R.id.ViewAnimator);
    // Switch to the second layout
    va.showNext();
    // Add another layout at the third position
    LinearLayout fooLayout = new LinearLayout(this);
    va.addView(fooLayout, 3, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
    

提交回复
热议问题