尝试在网页浏览中添加广告,但广告只是随网页一起滚动

时间:2016-12-07 22:56:23

标签: android webview

起初我尝试使用Vertical Linear layot并在应用程序中添加了webview和adview,但应用程序崩溃了。下面的xml可以正常工作,但广告会随着静态网页一起滚动。 我是一般的编程新手,我在学校学习了一些HTML,所以我使用静态网页来编写应用程序,同时在java中使用webview。 我也试过这个: Align AdView (AdMob) to bottom of screen with WebView on Android Layout无济于事。我尝试使用谷歌搜索但似乎没有任何帮助。如何将广告修复到屏幕底部?这是我第一次尝试创建Android应用程序。

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:focusableInTouchMode="true">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>
</WebView>

我的java代码是:

public class Book extends AppCompatActivity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book);
    MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.loadUrl("file:///android_asset/book.html");
}

}

1 个答案:

答案 0 :(得分:0)

尝试一下

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

    <WebView 
       android:id="@+id/webview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/adView"
       android:layout_alignParentTop="true" />

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

</RelativeLayout>

爪哇

MobileAds.initialize(getApplicationContext(), "replace");
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("youtAdId");
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.google.pt");