如何同时将屏幕方向应用于纵向所有布局 android studio

How to apply screen orientation to portrait too all the layouts at once android studio

你好,我正在制作一个应用程序,我想要的是所有布局都应该锁定在 potrait inshrot 中,我希望我的应用程序在 potrait 中,即使用户尝试在横向中制作它

我有一些想法,但不知道是否可行

在我的应用程序中,我只有一个主 activity,其余的都是片段,所以我的想法是,如果我将 reqiredActivity 提供给主 activity 的 potrait,它会工作吗

现在我将每个片段 requiredAcitivity 赋予 potrait,但当我将来有很多片段时,这对我来说可能很麻烦

 requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

您可以在 activity 清单 XML 中进行设置。 screenOrientation.

<activity
            android:name=".Android_mobile_infoActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  </activity>

在所有activity

的Activity清单文件中
android:screenOrientation="portrait"

或创建一个基础Activity并添加

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

然后在所有活动中扩展它。

或者创建一个应用程序class

class MyApp: Application(), Application.ActivityLifecycleCallbacks {
    override fun onCreate() {
        super.onCreate()

        registerActivityLifecycleCallbacks(this);
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }
}

然后在application标签下添加.MyApp

<application
        android:name=".MyApp"
        .. 
        />