Android 11(SDK 版本 30)的自适应横幅 - Kotlin - 已弃用
Adaptive Banner for Android 11 (SDK Version 30) - Kotlin - Deprecated
下午好。这是我在社区的第一次咨询。我正在尝试使用官方文档实现自适应横幅,但该示例包含已弃用的术语,而且 Android Studio 提供的帮助让我感到困惑。
我在论坛上找了几个小时,但没有找到可以帮助我的答案。
如果您能帮助我使用 Kotiln 为 SDK 30 实现自适应横幅,我将不胜感激。
private lateinit var adView: AdView
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = frameAnuncio.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
Pic1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receta)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
MobileAds.initialize(this) { }
adView = AdView(this)
frameAnuncio.addView(adView)
loadBanner()
}
private fun loadBanner() {
adView.adUnitId = AD_UNIT_ID
adView.adSize = adSize
val adRequest = AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()
adView.loadAd(adRequest)
}
companion object {
private val AD_UNIT_ID = "ca-app-pub-3940256099942544~3347511713"
}
Pic2
Admob 依赖项是:
实施 'com.google.android.gms: play-services-ads: 19.6.0'
谢谢
对于第一部分,您可以使用 windowManager.currentDisplayMetrics
访问 window 边界。和resources.displayMetrics
获取密度。
private val adSize: AdSize
get() {
val metrics = windowManager.currentWindowMetrics
val density = resources.displayMetrics.density
var adWidthPixels = frameAnuncio.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = metrics.bounds.width().toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
添加测试设备的新方法在documentation here中有说明。
尝试使用它为文档中的原始代码片段提供相同的值
private AdSize getAdSize() {
DisplayMetrics outMetrics = getResources().getDisplayMetrics();
float widthPixels = getScreenWidth();
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
public int getScreenWidth() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return windowMetrics.getBounds().width() - insets.left - insets.right;
} else {
DisplayMetrics outMetrics = getResources().getDisplayMetrics();
return outMetrics.widthPixels;
}
}
下午好。这是我在社区的第一次咨询。我正在尝试使用官方文档实现自适应横幅,但该示例包含已弃用的术语,而且 Android Studio 提供的帮助让我感到困惑。 我在论坛上找了几个小时,但没有找到可以帮助我的答案。 如果您能帮助我使用 Kotiln 为 SDK 30 实现自适应横幅,我将不胜感激。
private lateinit var adView: AdView
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = frameAnuncio.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
Pic1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receta)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
MobileAds.initialize(this) { }
adView = AdView(this)
frameAnuncio.addView(adView)
loadBanner()
}
private fun loadBanner() {
adView.adUnitId = AD_UNIT_ID
adView.adSize = adSize
val adRequest = AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()
adView.loadAd(adRequest)
}
companion object {
private val AD_UNIT_ID = "ca-app-pub-3940256099942544~3347511713"
}
Pic2
Admob 依赖项是: 实施 'com.google.android.gms: play-services-ads: 19.6.0'
谢谢
对于第一部分,您可以使用 windowManager.currentDisplayMetrics
访问 window 边界。和resources.displayMetrics
获取密度。
private val adSize: AdSize
get() {
val metrics = windowManager.currentWindowMetrics
val density = resources.displayMetrics.density
var adWidthPixels = frameAnuncio.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = metrics.bounds.width().toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
添加测试设备的新方法在documentation here中有说明。
尝试使用它为文档中的原始代码片段提供相同的值
private AdSize getAdSize() {
DisplayMetrics outMetrics = getResources().getDisplayMetrics();
float widthPixels = getScreenWidth();
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
public int getScreenWidth() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return windowMetrics.getBounds().width() - insets.left - insets.right;
} else {
DisplayMetrics outMetrics = getResources().getDisplayMetrics();
return outMetrics.widthPixels;
}
}