2017-07-20 48 views
0

我想在LinearLayout內切片3個視圖。但Adview沒有出現。這是我的佈局文件。我已經給了權重達到10,並將其分成3個視圖。Android Adview沒有顯示

<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> 

任何想法我失蹤。提前致謝。

回答

0

如果您有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> 
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"); 
    } 
}); 
0

當你正在使用android:layout_weight="3"屬性爲LinearLayout ,應指定佈局高度爲0dp

android:layout_height="0dp" 

而且這個屬性:

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

僅用於RelativeLayout,不LinearLayout

0

設置高度0dp而不是wrap_content。如果你設置它的wrap_content,它會根據它的要求將屏幕大小變爲0dp並檢查。