如何在 Android 应用程序中将 AdView 推送到页面底部

How to push AdView to Bottom of Page in Android App

如何将 AdView (AdMob) 广告推送到 Android Activity 中的页面底部?这是我的 activity_main.xml 布局的代码。由于某种原因,它没有对齐到底部。我试过将 AdView 放入其中无法正常工作。任何帮助表示赞赏。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout_main"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <!--custome toolbar-->
    <include layout="@layout/tool_bar" />

    <!--Wifi name and state-->
    <LinearLayout
        android:id="@+id/linear_wifi_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/wifi_icon_id"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20" />

        <TextView
            android:id="@+id/wifi_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.60"
            android:textSize="20dp"
            android:gravity="center"
            android:textAlignment="center" />

        <ImageView
            android:id="@+id/scan_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20"
            android:src="@drawable/ic_keyboard_arrow_right_white_36dp"/>

    </LinearLayout>

    <!--Progess bar-->
    <ProgressBar
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:id="@+id/progress_bar" />

    <!--this section is for wifi/private network-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/result_local"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:text="Local Network:"
            android:textColor="@color/colorAccent"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/private_net_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textSize="16dp"/>

    </LinearLayout>

    <!--this section is for public ip info-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/result_public"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textColor="@color/colorAccent"
            android:text="Public Ip:"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/public_net_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:textSize="16dp"/>

    </LinearLayout>

    <!-- banner ads for AdsMob-->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:padding="5dp">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

其他视图之一必须填充剩余的 space,因此广告被推到底部。您可以通过将其添加到 AdView 的正上方来实现:

<!-- fill remaining space -->
<Space
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />

另请注意:权重不必加起来为 1。它仅与其他 children 相关。因此,在布局中使用权重 0.2、0.6 和 0.2 的地方,您也可以使用 1、3 和 1 来达到相同的效果。
如果只有一个 child 有重量,它会消耗所有剩余的 space,无论其值如何。