在AdLoaded上设置ListView边距

时间:2017-02-15 18:54:17

标签: android listview layout margin

我的主要活动中有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>

1 个答案:

答案 0 :(得分:0)

在ListView&amp;下面设置Adview在XML中将AdView可见性设置为GONE,将LinearLayout用作ListView的父级&amp; 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);
    }
});