android set the attributes of the horizontal and vertical screen

Method 1: Find the screenOrientation property corresponding to Activity in AndroidManifest

android:screenOrientation="landscape"

The screenOrientation has the following parameters:

"unspecified":默认值
The display direction is inferred by the system. The determined strategy is related to the device, so different devices will have different display directions.
"landscape":
Horizontal screen display (width ratio is longer
"portrait":
than height ) Vertical screen display (height ratio is longer than width)
"user":
User's current preference The direction
"behind":
of the activity is consistent with the direction of the activity below the activity (in the Activity stack)
"sensor":
, which is determined by a physical sensor. Suppose the user rotates the device and the screen will switch between landscape and portrait.
"nosensor":Ignore the physical sensor. This will not change as the user rotates the device (except for the "unspecified" setting).

Method two: set in java code

Set the horizontal screen code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏

Set the vertical screen code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏 

Since the horizontal screen has two horizontal methods, and this sentence for setting the horizontal screen, assuming that it is not the default horizontal screen orientation, it will rotate the already horizontal screen by 180°.

So we can first infer whether the screen has been landscaped. Assuming that it is not re-rotating, users will not think that the rotation is inexplicable! The code is as follows:

if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT){
    
    
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Guess you like

Origin blog.csdn.net/ambitionLlll/article/details/114139915