我如何设置它,使应用程序只在纵向模式下运行?我希望在应用程序运行时禁用横向模式。如何通过编程实现呢?


当前回答

对于Xamarin用户:

如果你把你所有的活动扩展到一个BaseActivity,只需添加:

this.RequestedOrientation = ScreenOrientation.Portrait;

这样问题就解决了。如果你想要任何特定的活动在横向覆盖OnActivityCreated。为:

this.Activity.RequestedOrientation = ScreenOrientation.Landscape;

其他回答

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


    //setting screen orientation locked so it will be acting as potrait
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}

有两种方法。

添加android:screenOrientation="portrait"在你的manifest文件 相应的活动 添加 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 在onCreate()方法中添加到您的活动

好吧, 我试了所有的答案,但在旧版本的安卓系统上都不行。 所以,最终的解决方案是将这段代码添加到setContentView上面的每个活动:

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

为每一个活动写这个到你的清单文件:

android:screenOrientation="portrait" 

Use:

android:screenOrientation="portrait" 

只需将这一行写在应用程序的清单文件中,在每个你想只显示在纵向模式的活动中。