Android Adview未显示

时间:2017-07-20 01:08:59

标签: android android-layout android-studio

我正在尝试在LinearLayout中切片3个视图。但Adview没有出现。这是我的布局文件。我已经将weightSum赋予10并将其分为3个视图。

<LinearLayout
    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:orientation="vertical"
    android:weightSum="10"
    tools:context="in.example.LandingActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@drawable/landing_header_image"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_weight="4"
        android:layout_height="wrap_content">
    </android.support.v4.view.ViewPager>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-1390609726414683/1349170451"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

知道我错过了什么。提前致谢。

4 个答案:

答案 0 :(得分:0)

确保添加此初始化

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

然后加载您的广告

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

并通过在adView

上添加AdListener来检查其是否已加载或失败
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
        Log.i("Ads", "onAdLoaded");
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
        Log.i("Ads", "onAdFailedToLoad");
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when an ad opens an overlay that
        // covers the screen.
        Log.i("Ads", "onAdOpened");
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.
        Log.i("Ads", "onAdLeftApplication");
    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.
        Log.i("Ads", "onAdClosed");
    }
});

答案 1 :(得分:0)

当您对android:layout_weight="3"内的子视图使用LinearLayout属性时,应将布局高度指定为0dp

android:layout_height="0dp"

此属性:

android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"

仅用于RelativeLayout,而不是LinearLayout

答案 2 :(得分:0)

设置高度0dp而不是wrap_content。如果你将它设置为wrap_content,则需要将屏幕大小作为其要求,因此将其更改为0dp并进行检查。

答案 3 :(得分:0)

如果您有3个视图,并且想要将它分配到顶部,中间和底部..最好使用相对布局,以便您可以使用以下内容轻松创建它。

true