How can I set Orientation Fixed for all activities

后端 未结 5 1980
半阙折子戏
半阙折子戏 2020-12-09 23:20

Android Layout. How can I set Orientation Fixed for all activities in application Tag of AndroidMainfest.xml ? I don\'t want to set orientation for each activity individuall

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 00:10

    The accepted answer, and anything suggesting setRequestedOrientation, is far from perfect, because, as stated in documentation, calling setRequestedOrientation at runtime may cause the activity to be restarted, which among other things, affects animations between the screens.

    If possible, the best is to set the desired orientation in AndroidManifest.xml. But since it's error prone to rely on each developer to remember to modify the manifest when adding a new activity, it can be done at build time, by editing AndroidManifest file during the build.

    There are some caveats to editing AndroidManifest this way that you need to be aware of though:

    • If you have some entries in the output manifest, you should match instead of to avoid modifying those (and use replaceAll, which matches regex, instead of replace, which matches string)
    • Be careful to not match the activities that are out of your control. See facebook + android : Only fullscreen opaque activities can request orientation

    My requirement was to update all activities to have fixed orientation, but only in release builds. I achieved it with a bit of code in build.gradle which does simple string replacement in AndroidManifest (assuming that none of the activities has orientation specified already):

    Android Studio 3.0 compatible solution example (touching only activities that match com.mycompany.*):

    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            if (output.name == "release") {
                output.processManifest.doLast {
                    String manifestPath = "$manifestOutputDirectory/AndroidManifest.xml"
                    def manifestContent = file(manifestPath).getText('UTF-8')
                    // replacing whitespaces and newlines between `` and `android:name`, to facilitate the next step
                    manifestContent = manifestContent.replaceAll("

    Android Studio 2.3 compatible solution example (matching all activities, but not matching entries):

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (output.name == "release") {
                output.processManifest.doLast {
                    def manifestOutFile = output.processManifest.manifestOutputFile
                    def newFileContents = manifestOutFile.getText('UTF-8')
                            .replaceAll(/

    I used userPortrait instead of portrait as I prefer to give the user more flexibility.

    The above works out of the box if you just have variants (debug, release). If you additionally have flavors, you might need to tweak it a bit.

    You might want to remove if (output.name == "release") depending on your needs.

提交回复
热议问题