简体   繁体   中英

Android: Screen Rotation within Activity doesnt switch portrait/landscape layout

I build my Android Application and want do add layouts for different orientations now. I created a layout-land folder and put a different layout for my first Starter Activity "myStartActivity" (with the same name as the layout i used before for both orientations) in there.

Depending on my Screen Orientation BEFORE i start the app the right layout is chosen: "myLayout.xml" from within the "layout"-folder when i start in portrait and the "myLayout.xml" from within the "layout-land"-folder when i start in landscape.

The Problem is, that when i rotate the device when I'm already in the Activity, after rotation I dont get the new layout. For example: rotating from portrait to landscape it stills shows "myLayout.xml" from within the "layout"-folder and not the "layout-land"-folder as it should.

I didnt overwrite any OnConfigurationChange Methods or anything. All I do in "myStartActivity" is instantiate some buttons and give them some listeners. I wanna use a different layout in landscape to change the ordering of the buttons.

In my case the described problem happens only when I have android:configChanges="orientation" in the activity in the manifest.

Otherwise the correct layout is automatically used when rotating.

What you could do is use Activity.getResources().getConfiguration().orientation

Then depending on the orientation result, set the content view in your oncreate() method which is called on rotation.

protected void onCreate(Bundle savedInstanceState) {
int result = this.getResources().getConfiguration().orientation;
if (result == 1){
//set content view to portrait
setContentView(R.layout.portrait);
}
else{
//set content view to landscape} 
setContentView(R.layout.landscape);
}

Or stick in a case statement :)

If you're testing on the emulator only, there may be problems with detecting orientation change. I have experienced that at least.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM