屏幕旋转更改设置
在我的 Android 应用程序中,我有 2 种布局,一种用于纵向,一种用于横向。
在这两种布局中,我都有一个 TextView,用户可以通过单击来更改它,默认情况下它显示为“ON” 如果用户按下它,它就会变为“关闭”,一切都很好。
但是,如果将其设置为“关闭”并且旋转屏幕,则文本视图将重置为“打开”。我怎样才能阻止这种情况发生?
非常感谢
In my Android app I have 2 layouts, one for portrait and one for landscape.
In both layouts I have a TextView that the user can change by clicking on, by default it reads 'ON'
If the user presses it then it changes to 'OFF' All well and good.
However if it's set to OFF and the screen is rotated the text view resets to ON. How can I stop this from happening?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了这个问题每天都会被问到之外,答案是查看有关 AndroidManifest.xml 的 configChanges 它允许您在设备配置更改时覆盖默认行为,方向更改的默认行为是应用程序重新启动。
这里使用了添加到 AndroidManifest 中的通用行:
然后,如果您想对方向更改执行任何特定操作,则需要重写 Activity 方法 onConfigurationChanged()
Apart from the fact that this question is asked every single day, the answer is to look into configChanges with regards to AndroidManifest.xml It allows you to override the default behaviour when the devices config changes, the default behaviour for a change of orientation is that the app restarts.
A common line to add into the AndroidManifest is used here:
Then if you want to perform any specific action on the orientation change you need to override the Activity method onConfigurationChanged()