【问题标题】:How to apply screen orientation to portrait too all the layouts at once android studio如何一次将屏幕方向应用于纵向所有布局 android studio
【发布时间】:2021-09-20 21:54:52
【问题描述】:

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

我有一些想法,但不知道它是否有效

在我的应用程序中,我只有一个主要活动,其余的都是片段,所以我的想法是,如果我将 reqiredActivity 赋予 potrait 到主要活动,它会起作用

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

 requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

【问题讨论】:

    标签: android android-fragments android-activity screen-orientation portrait


    【解决方案1】:

    您可以在活动清单 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>
    

    【讨论】:

    • 看例子
    【解决方案2】:

    在所有活动的活动清单文件中

    android:screenOrientation="portrait"
    

    或创建一个 BaseActivity 并添加

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

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

    或者创建一个应用类

    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"
            .. 
            />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多