android - 在 Android Studio 中将 Admob 横幅调整为屏幕大小

标签 android admob banner screen-size

我最近开始在我的 Android 应用程序中使用 admob 横幅广告。它运行得很好,但我有一些问题。就目前而言,我使用的是BANNER,而不是SMART_BANNER。我的横幅适用于大多数手机,除了一小部分。在我的模拟器上,我可以看到当横幅对于屏幕来说太大时会出现问题。

我尝试使用智能横幅,但我仍然在 logcat 中收到相同的消息:“横幅需要 340x50。只得到 288x460”。我的横幅适用于大多数 5 英寸屏幕(部分屏幕除外)。但是当我在较小的屏幕尺寸上尝试该应用程序时,横幅不可见。

所以我的问题是:如何调整横幅大小以适应所有屏幕尺寸?

这是我用来创建广告横幅的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.percent.PercentRelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/timer1background2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="127dp"
            android:id="@+id/TimerTextview1"
            android:textColor="@android:color/black"
            android:textStyle="normal|bold"
            app:layout_marginTopPercent="11%"
            android:text="APP NAME"
            android:textSize="45dp" />

        <Button
            android:text="Go To Timer 1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/StartB1"
            android:textColor="@android:color/background_light"
            android:background="@color/colorPrimaryDark"
            android:textStyle="normal|bold"
            android:textSize="35sp"
            app:layout_marginTopPercent="33%"
            app:layout_heightPercent="15%"/>

        <Button
            android:text="Go To Timer 2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/StartB2"
            android:textColor="@android:color/background_light"
            android:background="@color/colorPrimaryDark"
            android:textStyle="normal|bold"
            android:textSize="35sp"
            app:layout_marginTopPercent="58%"
            app:layout_heightPercent="15%"/>

    </android.support.percent.PercentRelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="MY KEY">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

还有:

//Load ads
MobileAds.initialize(getActivity().getApplicationContext(), "MY KEY");

AdView adView = (AdView) myView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

最佳答案

使用具有动态广告尺寸的SMART_BANNER(屏幕宽度 x 32|50|90)

将 ads:adSize="BANNER" 替换为 ads:adSize="SMART_BANNER"

从这里阅读更多信息Banner Ads|Mobile Ads SDK (Android)

关于android - 在 Android Studio 中将 Admob 横幅调整为屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327730/

相关文章:

html - 在定时幻灯片中制作横幅背景图像过渡

Android Room `Insert` - 如何通过比较对象字符串来验证我不会输入相同的对象?

ios - Unity Game 不会使用 GoogleMobileAds 构建

android - 彻底移除 AdView

php - 优化我的随机行获取

css - 如何让我的图像标题调整大小?它在移动设备上被切断,但在桌面上看起来不错

android - 实现加速度计(Andengine)时出现问题

android - 无法在android中构建caffe

android - Flutter RTL AppBar 图标位置

firebase - 用于 flutter 的 firebase_admob 0.6.1 的 App ID 问题