Dynamically load layout in Android

2019-06-11 16:51发布

My application requires 2 screens and for this I have created two different XML layout files using RelativeLayout. One layout file loads whenever I run my Activity. Now I want to load the second layout on to the same Activity , when user click on a button in OptionsMenu and also when user press Back button the first screen loads instead of exiting the application. So that i don't need to create another Intent in my application.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-11 17:40

I would suggest a rather simpler means.

Put both your layouts in the single XML and show/gone them appropriately as need be. I dont think u need anything more complicated :-)

查看更多
该账号已被封号
3楼-- · 2019-06-11 17:42

Did you try re-calling the setContentView?

Or you may prefer using the ViewFlipper,

Good example here.

查看更多
该账号已被封号
4楼-- · 2019-06-11 17:53

Ideally there should be two different activities present in your application. You can add or remove a view component in a view but if you are looking for two completely different screens then i would suggest you to go for a new activity.

I dint get what you meant by "and also when user press Bakc button the first screen loads instead of exiting the application"

If you dont want to show the first screen just finish() the activity.

查看更多
登录 后发表回答