在 AdLoaded 上设置 ListView 边距

Set ListView margin on AdLoaded

我的主 activity 中有一个 AdMob 广告。我想更改项目的边距,以便在加载广告时它们不会重叠。

@Override
        public void onAdLoaded() {
            Toast.makeText(getApplicationContext(), "Ad is loaded!", Toast.LENGTH_SHORT).show();
            bannerHeight = mAdView.getHeight();
            FrameLayout fm = (FrameLayout) findViewById(R.id.containerView);
            DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams)fm.getLayoutParams();
            params.setMargins(0, 0, 0, bannerHeight);
            fm.setLayoutParams(params);
            ListView list = (ListView) findViewById(R.id.navList);
            RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams)list.getLayoutParams();
            params.setMargins(0, 0, 0, bannerHeight);
            list.setLayoutParams(params2);
        }

第一次更改,因此 containerView 有效,但 ListView 保持不变。我尝试在 xml 中给它 marginBottom 并且这也有效。我错过了什么?这是我的 xml:

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>


<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">
    </FrameLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">

        <!-- List of Actions (pages) -->
        <ListView
            android:id="@+id/navList"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:background="@color/busblue"/>

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

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

</RelativeLayout>

在 XML 中将 Adview 设置在 ListView 下方并将 AdView 可见性设置为 GONE,使用 LinearLayout 作为 ListView 和 Adview 的父级而不是 RelativeLayout

adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        adView.setVisibility(View.VISIBLE);
    }
    @Override
    public void onAdFailedToLoad() {
        super.onAdLoaded();
        adView.setVisibility(View.GONE);
    }
});