AdMob 自适应横幅未在 Jetpack Compose 中显示 [英] AdMob adaptive banner is not showing in Jetpack Compose

查看:23
本文介绍了AdMob 自适应横幅未在 Jetpack Compose 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在可组合组件上有一个 AdMob 常规横幅和一个自适应横幅.常规横幅显示正常,但自适应横幅显示不正常.任何人都可以帮忙解决这个问题吗?

I have an AdMob regular banner and an adaptive banner on a composable. The regular banner is showing alright, but the adaptive banner is not. Can anyone please help resolve this?

AdMob 参考

@Composable
fun AdNetworkApp() {
    val deviceCurrentWidth = LocalConfiguration.current.screenWidthDp.toInt()

    Column(
        modifier = Modifier
            .padding(16.dp)
            .fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text("Adaptive Banner")

        // shows an Adaptive banner test ad
        AndroidView(
            factory = { context ->
                AdView(context).apply {
                    adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
                        context,
                        deviceCurrentWidth
                    )
                    adUnitId = context.getString(R.string.ad_id_adaptive_banner)
                    loadAd(AdRequest.Builder().build())
                }
            }
        )

        Text("Regular Banner")

        // shows a banner test ad
        AndroidView(
            factory = { context ->
                AdView(context).apply {
                    adSize = AdSize.BANNER
                    adUnitId = context.getString(R.string.ad_id_banner)
                    loadAd(AdRequest.Builder().build())
                }
            }
        )
        ...

谢谢!

推荐答案

当您的横幅未显示时,您需要检查的第一件事是 adUnitId.

First thing you need to check when your banner is not showing is adUnitId.

如果传递给 adSize 的宽度太大,它也不会显示.但在您的情况下,您应用于 Columnpadding 阻止它显示全宽.

Also it can be not shown if the width passed to adSize is too big. But in your case, your padding applied to Column stops it from showing full width.

我建议你不要使用像 40 这样的未知常量.当您确定当前视图是顶部视图时,您可以计算列内的宽度,如 screenWidthDp - 2 * padding

I suggest you not using unknown constants like 40. When you're sure that current view is top one, you can calculate width inside column like screenWidthDp - 2 * padding

最准确"的是使用 onSizeChanged 获取列的宽度.

The most "accurate" is to get width of column with onSizeChanged.

我制作了一个示例来测试具有不同宽度的 AdView,但似乎都没有效果.但我建议你检查一个真正的 adUnitId 而不是测试一个,也许情况会更好.

I've made a sample to test AdView with different widths, and none seems working good. But I suggest you check out on a real adUnitId instead of the test one, maybe there situation is better.

val deviceCurrentWidth = LocalConfiguration.current.screenWidthDp
val padding = 16
var i by remember { mutableStateOf(0) }
var containerWidth by remember { mutableStateOf<Int?>(null) }
Column(
    horizontalAlignment = Alignment.CenterHorizontally,

    modifier = Modifier
        .padding(padding.dp)
        .fillMaxWidth()
        .onSizeChanged {
            containerWidth = it.width
        }
) {
    val items =
        listOf(
            "deviceCurrentWidth - 40" to deviceCurrentWidth - 40,
            "deviceCurrentWidth - padding * 2" to deviceCurrentWidth - padding * 2,
            "AdSize.FULL_WIDTH" to AdSize.FULL_WIDTH,
            "onSizeChanged" to with(LocalDensity.current) {
                containerWidth?.let { containerWidth ->
                    (containerWidth / density).roundToInt()
                }
            }
        )
    items.forEach {
        val (title, width) = it
        if (width == null) {
            return@forEach
        }

        Text(title)
        AndroidView(
            factory = { context ->
                AdView(context).apply {
                    adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
                        context,
                        width
                    )
                    adUnitId = "ca-app-pub-3940256099942544/6300978111"
                    loadAd(AdRequest.Builder().build())
                }
            },
            update = { adView ->
                adView.loadAd(AdRequest.Builder().build())
                i // needed to update view on i change
            },
        )
    }
    LaunchedEffect(Unit) {
        while (true) {
            delay(1000)
            i++
        }
    }
}

结果:

附言还有一点提示:使用修饰符作为最后一个参数:在这种情况下,您不需要使用逗号,因此添加和删除行变得更加容易

p.s. Also a little tip: use a modifier as the last argument: in this case you do not need to use a comma, so it becomes much easier to add and delete lines

Column(
    horizontalAlignment = Alignment.CenterHorizontally,
    modifier = Modifier
        .fillMaxWidth()
)

这篇关于AdMob 自适应横幅未在 Jetpack Compose 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆