开发者

How to change the screen orientation before layout / drawing happens?

开发者 https://www.devze.com 2023-04-07 04:45 出处:网络
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

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.


Simply define the orientation in your android-manifest xml

 <activity android:name=".MyActivity"
           android:screenOrientation="portrait">

If you are moving between Activities and you're get flickering due to that, add this xml attribute to your activity

 android:launchMode="singleTask"

Try setContentView using user pref in onCreate

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 boolean portrait = prefs.getBoolean(KEY_ORIENTATION, true);
 if (portrait) {
     setContentView(R.layout.myactivity);
 else {
     setContentView(R.layout-land.myactivity);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消