2017-07-20 114 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并检查。