如何让android横向布局在不刷新activity的情况下工作?
我已经看到了关于删除 android:configChanges 以使横向布局正常工作的答案,但这将刷新活动,我需要避免这种情况。一些答案建议以编程方式处理配置更改,但我不确定如何做到这一点。景观布局是否应该以编程方式创建?有谁知道如何解决这个问题?
I have seen answers regarding this with removing android:configChanges for the landscape layout to work but then this will refresh the activity and I need to avoid that. Some answers suggest programmatically handling config changes but I'm not sure how to do that. Should the landscape layout be created programmatically? Does anyone have any idea how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正确的答案是从清单中删除 android:configChanges ,并正确处理正在重新启动的活动。有一些技术可以解决您可能遇到的所有问题,并且除了旋转变化之外,还有许多其他事情可能导致它发生。
如果您绝对必须自己处理它,您可以通过覆盖 onConfigurationChanged 并检查方向是否发生变化来实现。但你不想这样做。如果您尝试自己执行此操作,那么您将遇到由于自己错误执行而导致的所有错误,以及由于您没有正确处理活动重新启动而未修复的所有错误。因此,忘记这一点,从清单中删除 configChange,并修复它,以便您可以处理重新启动。因为在您这样做之前,您的应用程序将变得一团糟,崩溃报告比您想象的还要多。
The correct answer is to remove the android:configChanges from your manifest, and correctly handle the activity being restarted. There's techniques to fix all the problems you may have with it, and there's dozens of other things that can cause it to happen anyway other than rotation changes.
If you absolutely must handle it yourself you can do so by overriding onConfigurationChanged and checking to see if it was the orientation that changed. But you don't want to do that. If you try to do it yourself, you're going to have all the bugs you cause by doing it yourself incorrectly and all the bugs you didn't fix because you aren't handling activity restarts correctly. So forget about this, remove the configChange from your manifest, and fix it so you cn handle restarts. Because your app is going to be a buggy mess until you do that, with more crash reports than you can imagine.
您可以为此创建新的布局文件,其名称与您的布局相同
并在可用的限定符中选择方向。
Android 管理一切。
you can make new Layout file for this, with the same name your layout
and choose Orientation in available qualifiers.
and Android manages everything.
如果您不想重新创建 Activity,则必须使用 android:configchanges 和方向。您必须在 onConfigurationChanged 方法中以编程方式处理布局。它可能很复杂,因为您不允许系统处理它。您必须以编程方式设置对齐、参数和所有其他结构。您将获得现有肖像模式元素的参考,并且必须自己更改它们的位置和其他内容。
If you dont want the activity to be recreated, then you have to use android:configchanges with orientation. You will have to handle the layout programmatically in onConfigurationChanged method. It can be complicated since you are not allowing system to handle it. You will have to set the alignment,parameters and all other structure programmatically. You will get reference of existing portrait mode elements and have to change their position and other things yourself.
您可以使用 OrientationEventListener 监听方向变化,
这是 kotlin 中的示例代码,它将监听方向变化并检查它是横向还是纵向模式,
private var mOrientationEventListener: OrientationEventListener? = null
private val THRESHOLD = 30
private var isLandScape = false
根据横向/纵向模式,您可以通过 setRotation 属性旋转视图。
You can listen for orientation changes using OrientationEventListener,
Here is a sample code in kotlin which will listen for orientation change and check it is landscape or portrait mode,
private var mOrientationEventListener: OrientationEventListener? = null
private val THRESHOLD = 30
private var isLandScape = false
According to landscape/portrait mode you can rotate your views by setRotation property.