简体   繁体   English

Android:活动中的屏幕旋转不会切换纵向/横向布局

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

I build my Android Application and want do add layouts for different orientations now. 我构建了我的Android应用程序,并希望现在为不同的方向添加布局。 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. 我创建了一个layout-land文件夹,并为我的第一个Starter Activity“myStartActivity”(与之前用于两个方向的布局同名)放置了不同的布局。

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. 取决于我的屏幕方向在我启动应用程序之前,选择正确的布局:“myLayout.xml”来自“layout”文件夹,当我以纵向开始时,“myLayout.xml”来自“layout-land” - 我在风景中开始的文件夹。

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. 例如:从纵向旋转到横向,它仍然在“布局”文件夹中显示“myLayout.xml”,而不是“layout-land”文件夹。

I didnt overwrite any OnConfigurationChange Methods or anything. 我没有覆盖任何OnConfigurationChange方法或任何东西。 All I do in "myStartActivity" is instantiate some buttons and give them some listeners. 我在“myStartActivity”中所做的只是实例化一些按钮并给它们一些听众。 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. 在我的情况下,只有当我在清单中的活动中有android:configChanges =“orientation”时才会出现所描述的问题。

Otherwise the correct layout is automatically used when rotating. 否则,旋转时会自动使用正确的布局。

What you could do is use Activity.getResources().getConfiguration().orientation 你可以做的是使用Activity.getResources()。getConfiguration()。orientation

Then depending on the orientation result, set the content view in your oncreate() method which is called on rotation. 然后根据方向结果,在oncreate()方法中设置内容视图,该方法在旋转时调用。

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. 我至少经历过这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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