Dynamically load layout in Android

巧了我就是萌 提交于 2020-01-03 04:49:09

问题


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.


回答1:


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.




回答2:


Did you try re-calling the setContentView?

Or you may prefer using the ViewFlipper,

Good example here.




回答3:


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 :-)



来源:https://stackoverflow.com/questions/4231797/dynamically-load-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!