Android:活动中的屏幕旋转不会切换纵向/横向布局
我构建了我的 Android 应用程序,现在想要添加不同方向的布局。我创建了一个布局土地文件夹,并为我的第一个入门活动“myStartActivity”放置了一个不同的布局(与我之前在两个方向上使用的布局同名)。
根据我启动应用程序之前的屏幕方向,选择正确的布局:当我以纵向方式启动时,从“layout”文件夹中选择“myLayout.xml”,从“layout-land”中选择“myLayout.xml”-当我以横向模式启动时的文件夹。
问题是,当我已经在活动中时旋转设备时,旋转后我没有得到新的布局。例如:从纵向旋转到横向,它仍然显示“layout”文件夹中的“myLayout.xml”,而不是应有的“layout-land”文件夹。
我没有覆盖任何 OnConfigurationChange 方法或任何内容。我在“myStartActivity”中所做的就是实例化一些按钮并为它们提供一些侦听器。我想在横向中使用不同的布局来更改按钮的顺序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我而言,仅当我有 android:configChanges="orientation" 时才会发生所描述的问题
在清单中的活动中。
否则,旋转时会自动使用正确的布局。
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.
你可以做的是使用 Activity.getResources().getConfiguration() .orientation
然后根据方向结果,在旋转时调用的 oncreate() 方法中设置内容视图。
或者坚持案例陈述:)
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.
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.