【问题标题】:Set ListView margin on AdLoaded在 AdLoaded 上设置 ListView 边距
【发布时间】:2017-07-04 14:43:12
【问题描述】:

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

【问题讨论】:

    标签: android listview layout margin


    【解决方案1】:

    在 ListView 下方设置 Adview 并在 XML 中将 AdView Visibility 设置为 GONE,使用 LinearLayout 作为 ListView 和 Adview 的 Parent 而不是 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);
        }
    });
    

    【讨论】:

    • 这对我不好,因为当广告加载时它会覆盖列表视图的最后一行。
    • 尝试线性布局
    • 这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多