在 xml 中设置变量,就像在 android 中为 adMob 设置清单占位符一样
Set variables in xml just like manifest placeholders in android for adMob
build.gradle
文件中的清单占位符允许您指定常量,然后可以像这样在 manifest
文件中使用。
manifestPlaceholders = [admob_app_id: "ca-app-pub-3940256099942544~3347511713", banner_id: "ca-app-pub-3940256099942544/6300978111"]
然后在清单文件中
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="${admob_app_id}" />
这似乎不适用于布局 xml 个文件
<com.google.android.gms.ads.AdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/publisherAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="${banner_id}" />
有没有一种方法可以根据构建配置动态更改 banner id
。我试过以编程方式进行,但 admob throws 一直在抱怨,除非 adSize
和 adUnitId
都在 xml.
中声明
您可以使用 the resValue()
method 创建带有您的横幅 ID 的字符串资源,然后在您的布局中引用它:
android {
...
buildTypes {
release {
resValue("string", "banner_id", "foo")
...
}
debug {
resValue("string", "banner_id", "bar")
...
}
}
}
ads:adUnitId="@string/banner_id"
build.gradle
文件中的清单占位符允许您指定常量,然后可以像这样在 manifest
文件中使用。
manifestPlaceholders = [admob_app_id: "ca-app-pub-3940256099942544~3347511713", banner_id: "ca-app-pub-3940256099942544/6300978111"]
然后在清单文件中
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="${admob_app_id}" />
这似乎不适用于布局 xml 个文件
<com.google.android.gms.ads.AdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/publisherAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="${banner_id}" />
有没有一种方法可以根据构建配置动态更改 banner id
。我试过以编程方式进行,但 admob throws 一直在抱怨,除非 adSize
和 adUnitId
都在 xml.
您可以使用 the resValue()
method 创建带有您的横幅 ID 的字符串资源,然后在您的布局中引用它:
android {
...
buildTypes {
release {
resValue("string", "banner_id", "foo")
...
}
debug {
resValue("string", "banner_id", "bar")
...
}
}
}
ads:adUnitId="@string/banner_id"