我如何禁用景观模式的一些观点在我的Android应用程序?


当前回答

在AndroidManifest.xml文件中添加android:screenOrientation="portrait"。

例如:

<activity 
    android:name=".MapScreen"
    android:screenOrientation="portrait"></activity>

其他回答

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

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

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

不要忘记添加app:name="。MyApp”。

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);
            }
            ....
        });
    }
}

你可以通过在manifest.xml文件中这样写来强制你的特定活动始终保持纵向模式:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"></activity>

你也可以通过在你的活动的onCreate()方法中写入下面的行来强制你的活动保持在纵向模式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

这对我很管用。尝试在AndroidManifest文件中添加以下代码:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    ....
    ....
</application>

将android:screenOrientation="portrait"添加到AndroidManifest.xml中的活动中。例如:

<activity android:name=".SomeActivity"
          android:label="@string/app_name"
          android:screenOrientation="portrait" />

由于这已经成为一个超级流行的答案,我感到非常内疚,因为强制肖像很少是它经常应用的问题的正确解决方案。 强制肖像的主要注意事项:

This does not absolve you of having to think about activity lifecycle events or properly saving/restoring state. There are plenty of things besides app rotation that can trigger an activity destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and retainInstance fragments. Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."

所以大多数应用程序应该让手机传感器、软件和物理配置自己决定用户想要如何与你的应用程序交互。但是,如果你对你的用例中传感器方向的默认行为不满意,你可能仍然需要考虑一些情况:

If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using nosensor for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them). If you still need to force portrait for some reason, sensorPortrait may be better than portrait for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.

你所需要的就是AndroidManifest.xml中活动的以下属性:

android:configChanges="orientation"

那么,完整的活动节点:

<activity
    android:name="Activity1"
    android:icon="@drawable/icon"
    android:label="App Name"
    android:configChanges="orientation">