因为 WebView 是一个 AbsoluteLayout,所以您可以像这样定位它的子视图:
AbsoluteLayout.LayoutParams params = AbsoluteLayout.LayoutParams(width,height,X-position,Y-position);
adView.setLayoutParams(params);
但是,将 WebView 放在 LinearLayout 中会更容易,因此当您添加广告时,它会自动出现在底部。例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
您的代码必须更改为如下所示:
private AdView adView;
adView = new AdView(this, AdSize.BANNER, "my publisher id");
LinearLayout root = (LinearLayout)findViewById(R.id.main);
root.addView(adView);
adView.loadAd(new AdRequest());
另一种选择是在 xml 中包含广告。您可以在 AdMob https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android开发者指南中了解如何执行此操作 。取自网站:
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- WebView goes here -->
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>