向上推 admob 广告的键盘

2022-01-25 00:00:00 android java admob

我有一个安卓应用程序.我刚刚将示例广告添加到我的应用程序中,但我的 EditText 上的广告一直存在问题.

I have an android application. I just added the sample advertisments to my app, and I keep having problems with the ad over my EditText.

当我单击 EditText 时,屏幕底部的广告会向上移动到键盘上方.

When I click on the EditText, the advertisment which is at the bottom of the screen moves up above the keyboard.

我的代码如下:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context="org.alexwebber.frc.strongholdcalculator.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/autonomous"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_alignParentStart="true"
        android:id="@+id/autoLabel" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/autogrid"
        android:layout_below="@+id/autoLabel"
        android:layout_alignParentStart="true"
        android:layout_marginTop="1dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/defreached"
            android:id="@+id/defenseReached"
            android:textSize="17sp"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnSpan="13" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/defcrossed"
            android:textSize="17sp"
            android:id="@+id/defcrossed"
            android:layout_row="1"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/lowgoal"
            android:textSize="17sp"
            android:id="@+id/lowgoalauto"
            android:layout_row="2"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/highgoal"
            android:textSize="17sp"
            android:id="@+id/highgoalauto"
            android:layout_row="3"
            android:layout_column="0" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/autodefreach"
            android:textSize="12sp"
            android:layout_row="0"
            android:layout_column="24"
            android:enabled="true"
            android:text="@string/total" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/autodefcross"
            android:textSize="12sp"
            android:enabled="true"
            android:text="@string/total"
            android:layout_row="1"
            android:layout_column="24" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/autolowgoal"
            android:enabled="true"
            android:text="@string/total"
            android:textSize="12sp"
            android:layout_row="2"
            android:layout_column="24" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/autohighgoal"
            android:textSize="12sp"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="3"
            android:layout_column="24" />
    </GridLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/teleop"
        android:id="@+id/teleopLabel"
        android:layout_below="@+id/autogrid"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="0dp" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="175dp"
        android:layout_below="@+id/teleopLabel"
        android:layout_alignParentStart="true"
        android:layout_marginTop="1dp"
        android:id="@+id/gridLayout">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/defcrossed"
            android:id="@+id/defcrossedtele"
            android:textSize="17sp"
            android:layout_row="0"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lowgoal"
            android:id="@+id/lowgoaltele"
            android:textSize="17sp"
            android:layout_row="1"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/highgoal"
            android:id="@+id/highgoaltele"
            android:textSize="17sp"
            android:layout_row="2"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/challegedTower"
            android:id="@+id/challengestower"
            android:textSize="17sp"
            android:layout_row="3"
            android:layout_column="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/scaledTower"
            android:id="@+id/scaledtowertele"
            android:textSize="17sp"
            android:layout_row="4"
            android:layout_column="0" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/teledefcross"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="0"
            android:textSize="12sp"
            android:layout_column="13" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/telelowgoal"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="1"
            android:textSize="12sp"
            android:layout_column="13" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/telehighgoal"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="2"
            android:textSize="12sp"
            android:layout_column="13" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/telechallengedtower"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="3"
            android:textSize="12sp"
            android:layout_column="13" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/telescaledtower"
            android:text="@string/total"
            android:enabled="true"
            android:layout_row="4"
            android:textSize="12sp"
            android:layout_column="13" />

    </GridLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/gridLayout"
        android:id="@+id/rpgrid">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/breach"
            android:id="@+id/breach"
            android:layout_marginTop="1dp"
            android:checked="false"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_below="@+id/gridLayout"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/capture"
            android:id="@+id/captured"
            android:layout_marginTop="1dp"
            android:checked="false"
            android:layout_row="0"
            android:layout_column="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/calculate"
            android:id="@+id/button"
            android:textSize="20sp"
            android:layout_row="1"
            android:layout_column="0"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/totalscore"
            android:textSize="20sp"
            android:id="@+id/total"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_gravity="right" />
    </GridLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:windowSoftInputMode="adjustPan"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

推荐答案

因为你设置了alignParentBottom="true",所以它向上移动并与EditText重叠,因此当键盘显示向上,布局父底部是SoftKeyboard的顶部.

It moves up and overlaps EditText because you set alignParentBottom="true", thus when keyboard shows up, the layout parent bottom is the top of the SoftKeyboard.

您应该可以通过将所有内容 above 添加到 AdView 并在 ActivityadjustResize 来获得解决方案清单中的代码>声明.附带说明一下,我不确定在 AdView xml 的小部件中调用 android:windowSoftInputMode="adjustPan" 会做些什么......

You should probably get the solution by adding the all content above the AdView and by calling adjustResize on the Activity declaration in Manifest. On a side note, I'm not sure that calling android:windowSoftInputMode="adjustPan" in AdView xml's widget is going to do something...

因此,要处理此行为,请将 AdView 放在布局容器的顶部,并为其余内容声明一个子容器:

Therefore, to handle this behaviour, put the AdView at the top of layout's container and declare a sub-container for the rest of content:

<RelativeLayout ...>
    <AdView
        android:id="@+id/adView"
        android:layout_alignParentBottom="true"
        ... />
    <RelativeLayout
        android:layout_above="@id/adView"
        ...>
        <TextView ...>
        <GridLayout ...>
        <GridLayout ...>
    </RelativeLayout>
</RelativeLayout>

然后,在Manifest.xml中出现键盘时进行布局调整:

Then, make the layout adjusting when keyboard shows up in Manifest.xml:

<activity
    android:windowSoftInputMode="adjustResize"
    ... />

这应该按预期工作类似的答案.此外,还有另一种解决方法:动态检测键盘何时显示并使 AdView 不可见,反之亦然,但我认为它执行的硬代码太多而不是解析为布局.

This should work as expected on similar answer. Also, there is another workaround: dynamically detect when the Keyboard is showing up and make the AdView invisible, and vice versa, but I think it does too much hard code than resolved into layout.

相关文章