景观和肖像Android应用程序(Landscape and portrait android app)

我创建了两个文件夹布局和布局 - 带有两个xml文件,一个用于纵向,另一个用于横向。 两个xmls都可以工作,但问题就在这里。

我的第一个屏幕是登录屏幕,第二个屏幕是主屏幕。 如果我以纵向登录,然后在主屏幕上转动手机横向。 布局将横向转动,但它使用纵向xml作为主屏幕。

如果我在横向开始并尝试稍后移动到肖像,则会发生同样的错误。

这似乎是我为我的主要做的任何布局,那就是将用于应用程序其余部分的布局。 无论如何还有这个吗?

也。 我已经在我的清单中使用了android:configChanges =“orientation”来进行活动。

I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.

My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.

The same error occurs if I start in landscape and try to move to portrait later on.

It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?

Also. I'm already using android:configChanges="orientation" in my manifest for the activities.

最满意答案

如果您使用的是android:configChanges="orientation" ,则可以在配置更改后覆盖onConfigurationChanged以onConfigurationChanged新布局。

@Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(...); }

确保您有充分的理由阻止在方向更改时重新创建活动...最重要的是不要只是因为方向更改导致应用程序崩溃。 自己处理配置更改会使使用备用资源变得更加困难,因为系统不会自动为您应用它们。 当您必须避免由于配置更改而重新启动时,此技术应被视为最后的手段,并且不建议用于大多数应用程序。

If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.

@Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(...); }

Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

更多推荐