AdMob 广告与 Horizo​​ntalScrollView 重叠

AdMob Ad overlapping HorizontalScrollView

我是 Android 开发新手,将 AdMob 集成到我的绘画应用程序中。

我在应用程序中有自己的工具栏,当我们展示广告广告时,工具栏工具是重叠的,这个应用程序是使用水平方向开发的。

5.5寸手机显示效果不错,我们运行5寸手机的时候有些工具被广告重叠了

我想在这里期待什么,打开广告时我想在工具栏中制作滚动条以使用所有可用工具。

我在 onAdLoaded 和 onAdOpened 事件中尝试了以下选项:

mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {

    /* Option 1: refresh the attributes set by static */
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.toolbarLayout);
        int visibility = rl.getVisibility();
        rl.setVisibility(View.GONE);
        rl.setVisibility(visibility);

    /* Option 2: refresh the attributes set by static */
        hsv.invalidate();

    /* Option 3: refresh the attributes set by static */
    rl.setLayoutParams(new
                    RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));

    /* Option 4: refresh the attributes set by static */
    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.toolbar);
    HorizontalScrollView.LayoutParams params = (HorizontalScrollView.LayoutParams) hsv.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.adLayout);
    }

    @Override
    public void onAdOpened() {

    /* Tried all the above options here, after ad opened*/

    }
});

下面是我的视图代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/toolbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorToolbarBg"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <HorizontalScrollView
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="@dimen/scrollBarSize"
        android:layout_alignRight="@+id/adLayout"
        android:layout_alignEnd="@+id/adLayout"
        android:scrollbars="horizontal"
        android:fadeScrollbars="false">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <ImageButton
        android:id="@+id/new_button"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:contentDescription="@string/new_drawing"
        android:src="@drawable/ic_new_file"/>

    <ImageButton
        android:id="@+id/brush_button"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:contentDescription="@string/brush"
        android:src="@drawable/ic_brush"/>

    <ImageButton
        android:id="@+id/color_lens_button"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:contentDescription="@string/brush_color"
        android:src="@drawable/ic_lens"/>

    <ImageButton
        android:id="@+id/erase_button"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:contentDescription="@string/eraser"
        android:src="@drawable/ic_eraser" />

    <ImageButton
        android:id="@+id/save_button"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:contentDescription="@string/save"
        android:src="@drawable/ic_save"/>

        <ImageButton
            android:id="@+id/more_button"
            android:layout_width="50sp"
            android:layout_height="50sp"
            android:contentDescription="@string/more"
            android:src="@drawable/ic_more"/>
    </LinearLayout>
    </HorizontalScrollView>

    <RelativeLayout
        android:id="@+id/adLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:orientation="horizontal">

        <com.google.android.gms.ads.AdView
            android:id="@+id/bannerAdView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
    </RelativeLayout>
</RelativeLayout>

尝试将 HorizontalScrollView xml 块更改为:

<HorizontalScrollView
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="@dimen/scrollBarSize"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/adLayout"
        android:scrollbars="horizontal"
        android:fadeScrollbars="false">

toLeftOf 使工具部分出现在广告的左侧

  1. 交换 HorizontalScrollViewRelativeLayout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/toolbarLayout">
    
        <RelativeLayout android:id="@+id/adLayout">
            ..................
            .......................
        </RelativeLayout>
    
        <HorizontalScrollView android:id="@+id/toolbar">
            ..................
            .......................    
        </HorizontalScrollView>
    
    </RelativeLayout>
    
  2. 将属性 android:layout_alignParentLeft="true"android:layout_toLeftOf="@+id/adLayout" 添加到 HorizontalScrollView

  3. HorizontalScrollView 中删除先前的属性 android:layout_alignRight="@+id/adLayout"android:layout_alignEnd="@+id/adLayout"

这是完整的工作代码。试试这个:

<RelativeLayout
        android:id="@+id/toolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:id="@+id/adLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true">

            <com.google.android.gms.ads.AdView
                android:id="@+id/bannerAdView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>
        </RelativeLayout>

        <HorizontalScrollView
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/adLayout"
            android:scrollbars="horizontal"
            android:scrollbarSize="2dp"
            android:fadeScrollbars="false">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ImageButton
                    android:id="@+id/new_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_new_file"/>

                <ImageButton
                    android:id="@+id/brush_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_brush"/>

                <ImageButton
                    android:id="@+id/color_lens_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_lens"/>

                <ImageButton
                    android:id="@+id/erase_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_eraser" />

                <ImageButton
                    android:id="@+id/save_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_save"/>

                <ImageButton
                    android:id="@+id/erase_button_temp"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_eraser" />

                <ImageButton
                    android:id="@+id/save_button_temp"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_save"/>

                <ImageButton
                    android:id="@+id/more_button"
                    android:layout_width="50sp"
                    android:layout_height="50sp"
                    android:src="@drawable/ic_more"/>
            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>

输出:

ScrollBar 显示正常,Left-Right & Right-Left 滚动效果很好。

希望对你有所帮助~