簡體   English   中英

僅在發布版本中將 screenOrientation 設置為“縱向”

[英]Set screenOrientation to "portrait" in release build only

**AndroidManifest.xml**我有一個**Activity**縱向模式:

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

我發現在開發過程中臨時刪除android:screenOrientation="portrait"行以便更早地捕獲與保存實例狀態相關的錯誤很有用。

是否可以僅在發布版本中將屏幕方向設置為縱向並且對於調試版本保持默認行為?

它可以通過使用manifestPlaceholder屬性在應用程序的build.gradle文件中提及發布配置來實現,如下所示。 並將其從清單文件中刪除。 同樣對於調試版本,您也可以單獨指定。

android {
    ...
    buildTypes {
        release {
            ...
            manifestPlaceholders.screenOrientation = "portrait"
        }
        debug {...}
    }
}

要了解有關manifestPlaceholders更多信息並建立青睞,請參閱此處官方網站

將此添加到您的活動中,

if (!BuildConfig.DEBUG) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

您可以在調試版本中覆蓋部分 AndroidManifest。 src/debug創建另一個 AndroidManifest.xml,並告訴合並工具為您的活動替換 s​​creenOrientation 屬性:

    <activity
        android:name=".home.MainActivity"
        tools:replace="android:screenOrientation"
        android:screenOrientation="unspecified"/>

有關更多信息,請參閱 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM