2015-11-07 284 views
0

我在我的Android应用中添加横幅广告。广告被显示在布局文件的设计,但它不是在应用横幅广告不显示

这里显示的主课堂,我加入的AdView

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.activity_main_list); 
    AdView mAdView = (AdView) findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
    mAdView.loadAd(adRequest); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.maintitlebar); 
    loadPage(); 

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

这里是上面的Java文件的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
tools:context=".MainListActivity$PlaceholderFragment" 
android:padding="0dp" 
> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:listSelector="@drawable/list_selector" 
    android:layout_alignParentTop="true"> 

    </ListView> 
<TextView 
    android:id="@android:id/empty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:text="@string/no_text" 
    /> 
<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentBottom="true" 
    ads:adSize="BANNER" 
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> 
</com.google.android.gms.ads.AdView> 

</RelativeLayout> 

因此,如何在Android应用展示广告

登录猫错误以下

11-08 03:42:39.923 2519-2613/com.example.talha.appforblog W/Ads﹕ There was a problem getting an ad response. ErrorCode: 0 
11-08 03:42:39.931 2519-2519/com.example.talha.appforblog W/Ads﹕ Failed to load ad: 0 
+0

你看到什么是来自AdMob在logcat的反应?另外 - ListView是否覆盖整个屏幕? –

+0

是的,Listview只覆盖整个屏幕和标题栏。我在上面的代码中编辑了Log Cat响应。 PLease看看它 – user3585510

回答

0

尽量给横幅广告的一些空间,这样的事情:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/child_r1"> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="400dp" 
    android:id="@+id/listView_" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:transitionGroup="false" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/child_r2" 
    android:gravity="center_vertical|center|center_horizontal" 
    android:layout_below="@+id/child_r1"> 


    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="ca-app-pub-3940256099942544/6300978111" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp"> 
    </com.google.android.gms.ads.AdView> 
</RelativeLayout> 

首先导入这两个在主:

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

二只放此代码为AD浏览:

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

第三个确定你有这个:

compile 'com.google.android.gms:play-services-ads:8.1.0' 

在你的 “buld.gradle” 下,类似这样的 “依赖” 的东西:

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 

compile 'com.android.support:appcompat-v7:22.2.1' 
compile 'com.google.android.gms:play-services-ads:8.1.0' 

} 
+0

有一个错误,说多个根标签 – user3585510

+0

看到编辑并尝试它...祝你好运 – dragon3002

0

尝试设置的ListView以上的AdView

设置android:layout_above属性的ListView。

事情是这样的:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:listSelector="@drawable/list_selector" 
    android:layout_alignParentTop="true" 
    ***android:layout_above="@+id/adView"***> 
    </ListView> 

<TextView 
    android:id="@android:id/empty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:text="@string/no_text" 
    /> 
<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentBottom="true" 
    ads:adSize="BANNER" 
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> 
</com.google.android.gms.ads.AdView> 

</RelativeLayout>