开发者

Android: switching screens with new activity or just changing content view

开发者 https://www.devze.com 2023-03-22 20:18 出处:网络
I am making a game where I will go from my main screen to a battle screen and the back when the battle is finished.Is it better to have the two screens a开发者_Go百科s separate activities or can I get

I am making a game where I will go from my main screen to a battle screen and the back when the battle is finished. Is it better to have the two screens a开发者_Go百科s separate activities or can I get by using the setContentView(R.layout.screen2)?

I've been trying to get the activity screen switching to work but everytime I try it, my phone tells me that the app isn't responding. If it is better to use the activities, then I'll take the time to work it out, but for now I'll take the easy way out to get things working.


Calling setContentView() multiple times is not recommended. The normal way to switch between layouts in the same activity is to use a ViewFlipper or FrameLayout (see Calling setContentView() multiple times and How can i make a dynamic flipping screen(like that of iPhone) in Android)

Alternatively you could use ViewSwitcher, which seems to fit your needs pretty well. There's a good detailed tutorial available at http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/


The simplest way to change view of an activity dynamically would be to inflate the target view into the activity's root ViewGroup (FrameLayout preferably). This is just a one line code

inflater.inflate(R.layout.target_view,(ViewGroup)findViewById(R.id.activity_root_viewgroup), true); 
0

精彩评论

暂无评论...
验证码 换一张
取 消