2017-04-24 125 views
1

我想在webview上顯示橫幅廣告。但不知道該怎麼做。Android admob在webview上顯示橫幅廣告

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/activity_ptv" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context="com.techndroid.cricketlivestreaming.Ptv"> 
 

 
    <com.google.android.gms.ads.AdView 
 
     android:id="@+id/adView" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerHorizontal="true" 
 
     ads:adSize="BANNER" 
 
     ads:adUnitId="@string/banner_ad_unit_id" /> 
 

 
    <WebView 
 
     android:layout_below="@+id/adView" 
 
     android:id="@+id/webStream" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" /> 
 

 
</RelativeLayout>

我想要的廣告展示佈局的頂部。

我認爲它落後於web視圖。我需要顯示在網頁視圖

+0

因爲你可以使用框架佈局 – jagteshwar751

回答

2

可以在RelativeLayout的表現出太多:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <WebView 
     android:id="@+id/myWebView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     ads:adSize="BANNER" 
     ads:adUnitId="ca-app-pub-3940256099942544/6300978111" 
     android:layout_width="match_parent" 
     android:background="@android:color/transparent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"> 
    </com.google.android.gms.ads.AdView> 

</RelativeLayout> 

在java中,只寫:

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

謝謝。有效。 –

+0

admob禁止在webview中顯示廣告 –

+0

如果您在您的網站上顯示廣告或自己的內容,它不會被禁止。但是,如果您在其他內容上展示廣告,例如您在應用中添加了facebook.com並開始展示廣告,則會禁止您的帳戶。爲此,Google可能會禁止您的開發者帳戶!所以小心:) –

0

對於解決方案,您可以使用框架佈局

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_ptv" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.techndroid.cricketlivestreaming.Ptv"> 

<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    ads:adSize="BANNER" 
    ads:adUnitId="@string/banner_ad_unit_id" /> 

<WebView 
    android:layout_below="@+id/adView" 
    android:id="@+id/webStream" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</FrameLayout > 
+0

不,它不會顯示 –

+0

增加怪物的高度,然後檢查,也需要一些時間來加載。 – jagteshwar751

0
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 




     <WebView 

      android:id="@+id/webStream" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_ad_unit_id" /> 
</FrameLayout>