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


当前回答

在oncreate()方法中添加一个类:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

其他回答

在<apphome>/platform/android目录下,创建AndroidManifest.xml(从生成的目录中复制它)。

然后添加android:screenOrientation="portrait"到所有的活动元素。

您必须设置每个活动的方向。

<activity
    android:name="com.example.SplashScreen2"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.Registration"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.Verification"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.WelcomeAlmostDone"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.PasswordRegistration"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>

在Kotlin中,同样可以使用下面的代码以编程方式实现:

requestedOrientation = 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"