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


当前回答

来自Android开发者指南:

"orientation" The screen orientation has changed — the user has rotated the device. Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations. "screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device). Added in API level 13.

所以,在AndroidManifest.xml文件中,我们可以放:

<activity
            android:name=".activities.role_activity.GeneralViewPagerActivity"
            android:label="@string/title_activity_general_view_pager"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            >
        </activity> 

其他回答

类似于Graham Borland的答案……但是如果你不想的话,你似乎不需要创建Application类…只需在项目中创建一个Base Activity

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

并且在你想使用Potrait Mode的地方扩展这个类而不是AppCompatActivity

public class your_activity extends BaseActivity {}

在config.xml中的<widget>下添加<preference name="orientation" value="portrait" />对我来说很有效。

(其他解决方案要么在我的设备上不起作用,要么在构建过程中被覆盖,要么在构建过程中给出了弃用错误。)

Use:

android:screenOrientation="portrait" 

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

您可以为整个应用程序这样做,而不必使所有活动扩展一个公共基类。

诀窍是首先确保在项目中包含Application子类。在它的onCreate()中,当你的应用程序第一次启动时调用,你注册了一个ActivityLifecycleCallbacks对象(API级别14+)来接收活动生命周期事件的通知。

这让你有机会在应用程序中的任何活动启动(或停止,或恢复,或其他)时执行自己的代码。此时,您可以在新创建的活动上调用setRequestedOrientation()。

class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();  

        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, 
                                          Bundle savedInstanceState) {

                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            ....

        });

    }
}

对于Xamarin用户:

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

this.RequestedOrientation = ScreenOrientation.Portrait;

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

this.Activity.RequestedOrientation = ScreenOrientation.Landscape;