How to enable only portrait and reverse portrait i

2019-04-28 02:13发布

问题:

I cant figure out why setting "sensorPortrait" attribute in manifest for an activity doesn't work to allow the activity to go into reverse portrait. This is how I set in manifest

    <activity
        android:name=".MainActivty"
        android:label="Testing"
        android:screenOrientation="sensorPortrait"
        android:windowSoftInputMode="stateHidden"
        android:configChanges="orientation">
    </activity>

The above code is not working for me to get both reversePortrait and portrait on Moto E(4.4.4) or Galaxy S3(4.3), but I can confirm it works on a Nexus 7(5.0.1) tablet, I tried several other answers but this one seems to be the easiest and it is seemingly not working for me on 'phones', am I missing some information that is relevant for getting this to work?

I tried the following link which is just as similar as my question but nothing seems helpful regarding this

Activity in portrait or reverse portrait only

Currently with the flag "sensorPortrait" it behaves just like setting "portrait", in phones.

回答1:

If you look at the PhoneWindowManager source code, you can see that it checks for the internal value config_allowAllRotations and uses that to determine if the 180 degree rotation (i.e., reverse portrait on phones) is allowed.

With a default value to false, this means that reverse portrait is disabled on phones. You'll note that the sw600dp value (i.e., tablets) is true, hence why you can use all rotations including reverse portrait on tablet devices.