2017-04-24 167 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>