如何在布局/绘图发生之前更改屏幕方向?
我想在子活动启动时设置其方向。方向必须在运行时设置,而不是在 XML 中。所以我把它的代码放在onCreate()中。我也有
android:configChanges="orientation|screenSize"
该活动的清单。但是当活动启动时,它首先以错误的方向绘制屏幕,然后进行更改。如何消除这种闪烁?
它与此线程有点相关:如何定义创建 Activity 之前的屏幕方向? 。另外,我知道我的活动只启动一次,因为我正在处理配置更改。
这是我在 onCreate 中的代码:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
orientation = sharedPref.getString("orientation_pref", "Auto");
setOrientation(orientation);
setContentView(R.layout.grid);
由于某种原因,它在加载 R.layout 文件后更改了方向。
I want to set the orientation of a subactivity when it starts. The orientation has to be set in run-time and not in XML. So I put the code for it in onCreate(). I also have
android:configChanges="orientation|screenSize"
in the manifest for the activity. But when the activity starts, it draws the screen in the wrong orientation first, then does the change. How do I eliminate this flikering?
It's kinda related to this thread: how to define the screen orientation before the activity is created? . Also, I know my activity only starts once since I am handling the config changes.
Here's my code in onCreate:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
orientation = sharedPref.getString("orientation_pref", "Auto");
setOrientation(orientation);
setContentView(R.layout.grid);
For some reason it changes the orientation after it loads the R.layout file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 android-manifest xml 中定义方向
如果您在 Activity 之间移动并且因此而出现闪烁,请将此 xml 属性添加到您的 Activity
尝试在 onCreate 中使用用户首选项 setContentView
Simply define the orientation in your android-manifest xml
If you are moving between Activities and you're get flickering due to that, add this xml attribute to your activity
Try setContentView using user pref in onCreate