2014-05-13 66 views
0

有像這樣的問題,但因爲我已經使用的xmlns它不是重複必需的XML屬性「adSize時」失蹤(谷歌播放服務)

:廣告=「http://schemas.android.com/ APK/RES-AUTO」 和我主要在Java

我想使用谷歌的廣告

發揮服務我用下面的Java代碼進行代碼

com.google.android.gms.ads.AdView adView = (com.google.android.gms.ads.AdView) activity.findViewById(R.id.adview); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    adView.setAdUnitId(activity.getString(R.string.admob_id)); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    adView.loadAd(adRequest); 

和以下XML代碼

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

但我在旗幟收到錯誤就像

Requred XML屬性 'adSize時' 失蹤

+0

嘗試添加AdView的中LinearLayout,首先採用線性佈局 – Rohit

+0

你有錯過廣告:xml – Nepster

回答

3

命名空間更改爲

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

廣告.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" /> 

</LinearLayout> 

注:

使用此命名空間爲新的SDK

xmlns:ads="http://schemas.android.com/apk/res-auto" 
+0

已經完成這樣的:(:( –

+0

偉大的答案,這是很難找到這一點,adSize =「BANNER」屬性。 +1 –

1

嘗試添加 「廣告:adSize時」,以XML象下面這樣:

<com.google.android.gms.ads.AdView 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/adview" 
    ads:adSize="SMART_BANNER" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
3

刪除「adView.setAdSize(adSize時.SMART_BANNER);」從你的java文件中。

請參考下:

這裏是XML佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<TextView android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"/> 
<com.google.android.gms.ads.AdView android:id="@+id/adView" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         ads:adSize="BANNER" 
         ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/> 
</LinearLayout> 

這裏是你的java文件

package com.google.example.gms.ads.xml; 

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

import android.app.Activity; 
import android.os.Bundle; 

/** 
* A simple {@link Activity} which embeds an AdView in its layout XML. 
*/ 
public class BannerXMLSample extends Activity { 

    private static final String TEST_DEVICE_ID = "INSERT_YOUR_TEST_DEVICE_ID_HERE"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // The "loadAdOnCreate" and "testDevices" XML attributes no longer available. 
    AdView adView = (AdView) this.findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .addTestDevice(TEST_DEVICE_ID) 
     .build(); 
    adView.loadAd(adRequest); 
    } 
} 

這裏指的例子 https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/banner-xml

+0

謝謝sumitarora,N –