简体   繁体   English

Android Adview没有显示

[英]Android Adview not showing up

I am trying to slice 3 views inside a LinearLayout. 我正在尝试在LinearLayout中切片3个视图。 But Adview is not showing up. 但是Adview没有出现。 Here is my layout file. 这是我的布局文件。 I have given weightSum to 10 and divided that among 3 views. 我已将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>

Any idea what I am missing. 知道我缺少什么。 Thanks in advance. 提前致谢。

Make sure you add this initialize 确保添加此初始化

MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

then load your ad 然后加载您的广告

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

and check if its loaded or failed by adding AdListener on your adView 并通过在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");
    }
});

When you are using android:layout_weight="3" attribute for child views inside of LinearLayout , you should specify layout height as 0dp : 当您在LinearLayout子视图中使用android:layout_weight="3"属性时,应将布局高度指定为0dp

android:layout_height="0dp"

Also this attributes: 此属性还具有:

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

are used only in RelativeLayout , not LinearLayout . 仅在RelativeLayout中使用,而不在LinearLayout

set the height 0dp instead of wrap_content. 设置高度0dp而不是wrap_content。 If you set it wrap_content it will take size of screen as its requirement so change it to 0dp and check. 如果设置了wrap_content,它将以屏幕的大小作为要求,因此将其更改为0dp并进行检查。

if you have 3 views and you want to devide it in top, center and bottom .. its better to use relative layout so that you can create it easily using following. 如果您有3个视图,并且想要在顶部,中央和底部进行划分,则最好使用相对布局,以便可以使用以下内容轻松创建它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@drawable/arrow_spinner"
            android:scaleType="fitCenter" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

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

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <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"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-1390609726414683/1349170451"></com.google.android.gms.ads.AdView>

    </LinearLayout>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM