开发者

Programmatically lock into portrait mode for certain operations

Is there a way to programmatically lock an app in portrait mode for certain operations, and then resume (and have the app rotate to landscape开发者_如何学Python if the user is holding the device that way) after the operation is complete?


Try this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Do your operation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);


I'm reading into your question a bit, but if the problem you are facing is that the reload of the activity causes problems, you can add in the manifest a line to handle the orientation changes yourself. You'll probably need to do this anyway if you want to do something special for the orientation changes, but perhaps just adding the lines will take care of your problems for you:

<activity android:name=".MyActivity"  
    android:configChanges="keyboard|keyboardHidden|orientation" />


Just use this in OnCreate Method of Activity if you want to set Screen only in Portrait

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

If you want only LANDSCAPE so use this line

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

This one Line is Enough

Like this below

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜