开发者

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

开发者 https://www.devze.com 2023-01-08 07:48 出处:网络
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

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开发者_如何学JAVA 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.

0

精彩评论

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

关注公众号