Android-智能横幅广告未在纵向模式下显示在屏幕上

时间:2017-09-04 09:23:39

标签: android admob

我在我的应用中使用了广告,并且我在此处遵循了教程:https://developers.google.com/admob/android/banner

如果我在xml中将adSize设置为“BANNER”,一切正常,但广告在横向模式或大屏幕上效果不佳。所以现在我正在尝试将adSize更改为“SMART_BANNER”,但由于某种原因横幅仅在横向模式下显示,而在肖像上我只有一个白色的广告。我在这里缺少什么?

以下是代码:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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:layout_margin="0dp"
    android:padding="0dp"
    tools:context="com.example.shaytsabar.footballtables
    .activities.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp"
    android:layout_marginLeft="0dp">


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_con"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:elevation="0dp"
    android:minHeight="?attr/actionBarSize"
    android:padding="0dp"
    android:paddingBottom="0dp"
    android:paddingEnd="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingStart="0dp"
    android:paddingTop="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<com.google.android.gms.ads.AdView 
 xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

<FrameLayout
    android:id="@+id/fragment_con"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:elevation="0dp"
    android:padding="0dp"
    app:layout_constraintBottom_toTopOf="@+id/adView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar_con"
    app:layout_constraintVertical_bias="0.0">

  </FrameLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity{
    InterstitialAd mInterstitialAd;
    AdView mAdView;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, APPCODE);
        ShowLittleAd();
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                ShowLittleAd();
            }
        });
      }
    public void ShowLittleAd(){
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new 
      AdRequest.Builder().addTestDevice("//MY DEVICE").
                addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
        mAdView.loadAd(adRequest);
    }          
}

1 个答案:

答案 0 :(得分:1)

好的..我遇到了问题......

请从您的Adview中移除所有边距。 替换此

<com.google.android.gms.ads.AdView 
 xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

到此 -

<com.google.android.gms.ads.AdView 
 xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

由于Banner现在可以使用全宽并且可以显示。问题是,边缘,显示宽度不够......