简体   繁体   English

Android:当我使用 android:configChanges=“orientation|keyboardHidden”> 时,横向模式的视图发生了变化

[英]Android: View of landscape mode changes when I use android:configChanges=“orientation|keyboardHidden”>

I'm developing my first app, an NBA Quiz.我正在开发我的第一个应用程序,一个 NBA 测验。

Here I have a Textfield with the Questions and four Buttons arranged horizontally.在这里,我有一个带有问题的文本字段和水平排列的四个按钮。 I then created a landscape mode and changed the structure of the Buttons to 2x2 (2 rows with 2 buttons).然后我创建了一个横向模式并将按钮的结构更改为 2x2(2 行,2 个按钮)。 At first I had some problems with different screen sizes but solved it with android:layout_weight="1".起初我遇到了一些不同屏幕尺寸的问题,但用 android:layout_weight="1" 解决了它。

When the landscape mode looked good I had another problem: Everytime I switched to landscape mode the Quiz started over (New Question, Score was zero again).当横向模式看起来不错时,我遇到了另一个问题:每次切换到横向模式时,测验都会重新开始(新问题,分数再次为零)。 I looked for this problem here at Stackoverflow and found the following solution:我在 Stackoverflow 上查找了这个问题,并找到了以下解决方案:

I changed the AndroidManifest.xml to我将 AndroidManifest.xml 更改为

[...]
<activity android:name=".QuizActivity"
     android:configChanges="orientation|keyboardHidden">
</activity>
[...]

and in QuizActivity.java在 QuizActivity.java

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

This solved the above problem.这解决了上述问题。 When I change to landscape mode the activity continues without starting anew.当我更改为横向模式时,活动会继续,而不会重新开始。 But now the 2x2 structure doesn't work anymore.但是现在 2x2 结构不再起作用了。

Here's a screenshot without the above code: landscape_mode_1这是没有上述代码的屏幕截图: landscape_mode_1

Here's a screenshot with the above code landscape_mode_2这是上面代码Landscape_mode_2的截图

Does anyone have an idea why the above code changes the structure of the landscape mode?有谁知道为什么上面的代码会改变横向模式的结构?

So the problem is with addition of android:configChanges="orientation|keyboardHidden" code your activity in not getting recreated on orientation changes (which solves your problem of loosing state etc.) but you lost the ability of using new layout (landscape) on orientation changes.所以问题在于添加android:configChanges="orientation|keyboardHidden"代码你的活动没有在方向变化时重新创建(这解决了你丢失 state 等的问题)但是你失去了使用新布局(风景)的能力方向变化。 When you define android:configChanges="keyboardHidden|orientation" in your AndroidManifest, you are telling Android: "Please don't do the default reset when the keyboard is pulled out, or the phone is rotated; I want to handle this myself."当您在 AndroidManifest 中定义 android:configChanges="keyboardHidden|orientation" 时,您是在告诉 Android:“请不要在拔出键盘或旋转手机时进行默认重置;我想自己处理。 "

Android destroys the Activity in orientation changes to inflate new layout (if any). Android 破坏方向更改中的 Activity 以膨胀新布局(如果有)。 So what you seeing after in landscape mode after the changes is the same layout (default/portrait) and not landscape one which is causing the button to appear horizontally instead of 2X2.因此,您在更改后在横向模式下看到的是相同的布局(默认/纵向),而不是横向的布局,这导致按钮水平显示而不是 2X2。

To save the state of activity across orientation changes using android:configChanges="orientation|keyboardHidden" is not recommended.不建议使用android:configChanges="orientation|keyboardHidden"保存跨方向更改的活动 state。 Instead you should use onSavedInstanceState bundle to retain state across orientation changes as apply them to the recreated activity.相反,您应该使用onSavedInstanceState包来保留 state 跨方向更改,以将它们应用于重新创建的活动。

Reference onSavedInstanceState: https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)参考 onSavedInstanceState: https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

Implementation: https://www.journaldev.com/22621/android-onsaveinstancestate-onrestoreinstancestate实现: https://www.journaldev.com/22621/android-onsaveinstancestate-onrestoreinstancestate

暂无
暂无

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

相关问题 将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗? - Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode? 为什么不总是使用 android:configChanges=&quot;keyboardHidden|orientation&quot;? - Why not use always android:configChanges=“keyboardHidden|orientation”? 在确定方向时,我应该如何使用android:configChanges - How should I use android:configChanges when determining orientation 如何在代码中写入“ android:configChanges =“ keyboardHidden””? - How do I write 'android:configChanges=“keyboardHidden”' in code? Android configChanges方向 - Android configChanges for orientation 纵向和横向方向,并在片段中设置android:configChanges =“ orientation” - portrait and landscape orientations and set android:configChanges=“orientation” in fragment android:configChanges =“ orientation”与surfaceChanged - android:configChanges=“orientation” with surfaceChanged 是否在设置configChanges =“ keyboardHidden | orientation”的情况下旋转调用onConfigurationChanged吗? - onConfigurationChanged not called at rotation with configChanges=“keyboardHidden|orientation” set? android:configChanges =“screenSize”参数在横向模式下采用纵向高度 - android:configChanges=“screenSize” parameter is taking portrait height in landscape mode android:configChanges,当方向更改时会调用onCreate中的异步SQLite调用 - android:configChanges with an Async SQLite Call in onCreate being called when orientation changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM