简体   繁体   English

Android-在onRestoreInstanceState期间操作视图位置

[英]Android - Manipulating view position during onRestoreInstanceState

I have made a simple dice game that makes use of the setTranslationY() method to indicate if a dice should be ignored for the next dice roll. 我制作了一个简单的骰子游戏,该游戏使用setTranslationY()方法来指示下一次掷骰子是否应忽略骰子。 This all works fine, except that when I try to restore the state of the app after a screen rotation, my restoring method cannot seem to use setTranslationY() on the views that need it. 一切正常,除了在屏幕旋转后尝试还原应用程序的状态时,我的还原方法似乎无法在需要它的视图上使用setTranslationY()

I have confirmed time and time again the the method IS being called for all views that should be nudged upwards a little bit, but the views stay where they are. 我已经一次又一次地确认,应该对所有应该向上微调的所有视图调用该方法,但是这些视图将保留在原处。

If it is so that the app needs to go through the process of completing onCreate() , onStart() , onRestoreInstanceState() and onResume() before being able to manipulate views then please advise on how to achieve this. 如果是这样,则应用程序需要先完成完成onCreate()onStart()onRestoreInstanceState()onResume()然后才能操作视图,然后请提供有关如何实现此目的的建议。 I need this to happen consistently upon every device rotation. 我需要在每次设备旋转时始终如一地发生这种情况。

You can retain the state of your application by informing Android you'd like to handle the logic for screen orientation yourself, rather than allowing the OS to trash and rebuild the entire Activity whenever the user tilts the screen. 您可以通过通知Android您希望自己处理屏幕方向的逻辑来保持应用程序的状态,而不是在用户倾斜屏幕时允许操作系统丢弃并重建整个Activity You can do this by adding the android:configChanges attribute to the Activity entry in your AndroidManifest.xml : 您可以通过将android:configChanges属性添加到AndroidManifest.xml的“ Activity条目来实现:

<activity android:name=".YourActivity"
          android:configChanges="orientation"
          android:label="@string/app_name">

This should hopefully preserve the state of your animated View s. 希望这将保留动画View的状态。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM