设置AndroidManifest的活动screenRotation每种风格不同

时间:2016-06-02 09:29:34

标签: android android-gradle android-manifest build.gradle

有没有办法在gradle的build.config中动态设置AndroidManifest中的活动screenOrientation?

我需要有一种允许旋转的味道,以及另一种只有肖像的味道。

我已阅读http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger,但他们的示例似乎仅适用于普通字符串标签。

我尝试了两种方法。

使用gradle的manifestPlaceholders: 的build.gradle:

productFlavors {
  flavorRotation {
    manifestPlaceholders = [ROTATION_PREF: "unspecified"]
  }
  flavorNoRotation {
    manifestPlaceholders = [ROTATION_PREF: "portrait"]
  }
}

使用AndroidManifest.xml:

...
  <activity android:name=".ui.ActivityName"
    android:screenOrientation="${ROTATION_PREF}"/>
...

这似乎不起作用,没有给出错误,但是当我构建&#34; flavorNoRotation&#34;

时,旋转没有被锁定为肖像

并尝试使用gradle的resValue: 的build.gradle:

productFlavors {
  flavorRotation {
    resValue "string", "orientation", "unspecified"
  }
  flavorNoRotation {
    resValue "string", "orientation", "portrait"
  }
}

with manifest:

...
<activity android:name=".ui.ActivityName"
  android:screenOrientation="@string/orientation"/>
...

通过这种方式,它构建得很好但是当我尝试安装到设备时它只是失败,Android Studio给出了消息: &#34;安装失败,消息为INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION。 通过卸载现有版本的apk(如果存在)然后重新安装,可能会解决此问题。&#34;

点击&#34; OK&#34;只是给出了一个事件日志错误&#34;错误安装APK&#34;。 那么,构建类型/口味之间有什么不同的方向吗?

我更愿意避免在BaseActivity中以编程方式执行此操作。

编辑: 对不起,我试过玩了一些,我试过的第一种方法确实有效。我必须在我自己的方面犯了一个错误,在我自己的设备上安装了错误的构建风格。

唯一的问题是Android Studio会在&#34; android:screenOrientation =&#34; $ {ROTATION_PREF}&#34;财产说它无法找到它,虽然它会建立起来并且有效。

感谢您的回答尝试。

4 个答案:

答案 0 :(得分:0)

1-Create screen.xml file inside res/values folder and code

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="screen_type">phone</string>
</resources>

2-In your activity inside onCreate() after setContentView() check device type

//checking tablets and phones
String screenType = getResources().getString(R.string.screen_type); if (screenType.equals("7-inch-tablet") || screenType.equals("10-inch-tablet")) {

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

} else {

   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} 

希望它会有所帮助。

答案 1 :(得分:0)

不那样。我所做的就是这个;创造一个父母&#34;活动类,您可以在其中检查要使用的布局类型,如下所示:(只需确保在子活动中调用超级优先)在这里,您可以检查构建版本。

public class DaddyActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set landscape or portrait
        if (getResources().getBoolean(R.bool.landscape)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}

它查找只有一个布尔值的XML:landscape。这就是XML(用于普通屏幕):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="landscape">false</bool>
</resources>

答案 2 :(得分:0)

分开源文件

离) ... / SRC / flavorRotation / RES /值/ strings.xml中

<string name="orientation">unspecified</string>

... / SRC /主/ RES /值/ strings.xml中

<string name="orientation">portrait</string>

答案 3 :(得分:0)

最好的方法是在gradle中添加2种口味,然后为“肖像”风味创建具有相同名称的目录并复制清单文件,并将screenOrientation添加到portrait

应用内gradle添加:

flavorDimensions "default"
productFlavors {
    any {
        applicationIdSuffix ".any"
    }

    portrait {
        applicationIdSuffix ".portrait"
    }
}

Add dir name "portrait" instead landscape as shown in image

然后在potrait目录的新清单文件中添加以下活动

        android:screenOrientation="portrait"
相关问题