如何为横幅制作 space

How to make a space for banner

布局简单,一个WebView和一个Banner(AD)。

我写了一个 XML 如下:

<LinearLayout
    android:id="@+id/banner_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</LinearLayout>

这是创建横幅运行时的代码。

// This code is copied & pasted from AudienceNetwork's guide page
adView = new AdView(this, "PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
adContainer.addView(adView);
adView.loadAd();

此代码无效,横幅显示在屏幕外。 所以,我要找的是 android:layout_height="match_parent - 50dp" 就像 CSS.

中的 calc

在 Android 中没有执行 android:layout_height="match_parent - 50dp" 的选项。

要整合 Facebook 受众网络,您需要先使用 banner_container 创建一个 xml。您可以考虑使用不同的布局结构,如下所示,使用 RelativeLayout

<?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="match_parent"
        android:layout_above="@+id/banner_container" />

    <LinearLayout
        android:id="@+id/banner_container"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" />
</RelativeLayout>

您可以使用现有布局,只需在 Webview 下面添加一个 LinearLayout。不要忘记在 Webview

中添加 android:layout_weight="1"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></WebView>

<LinearLayout
    android:id="@+id/banner_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

然后使用您现有的代码

adView = new AdView(this, "PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
adContainer.addView(adView);
adView.loadAd();