如何通过可编程方式将屏幕旋转为横向(或纵向)?

Fer*_*and 7 android

如何通过可编程方式将屏幕旋转为横向(或纵向)?我试图在用户旋转屏幕时使其独立.

有可能吗?

提前致谢.

sys*_*out 10

试试这个:

Activity.setRequestedOrientation() 
Run Code Online (Sandbox Code Playgroud)

使用这些参数:

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Run Code Online (Sandbox Code Playgroud)

检查这个以供进一步参考

  • 主页和PageUp在小键盘上,但请确保先关闭numlock. (3认同)

Vis*_*ath 6

您可以尝试下面的示例...

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // You can set the value initially by
    // ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED. Once set it
    // retains it's value. The View will be rendered in the specified
    // orientation using the code below.
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Run Code Online (Sandbox Code Playgroud)