当软键盘弹出时,adMob横幅覆盖了文本视图

6
我一直遇到一个问题,无法防止adMob横幅广告随着软键盘滚动并覆盖我的文本视图...。
这是我在Android中尝试实现的链接:http://bit.ly/myfoodcalc
我知道我的布局有点复杂,嵌套了太多视图(可能),等等。
我已经尝试过在清单上更改一些东西,但没有成功。
为了让您更好地理解,这里是一些伪XML(以及后面帖子中完整的XML)...
 <ScrollView>
      <Linear layout>
            <ImageView>
            <TableView>
                  <Several rows with a Edit Text widget>
            </TableView>
            </ImageView>
      </LinearLayout>
  </ScrollView>

  <FrameLayout>
       <ImageView>
  </FrameLayout>

这是我的完整XML代码:

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android" 
                    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" 
                    android:id="@+id/relativeLayout"
                    android:background="@drawable/backgroundwithtext">

    <ScrollView android:id="@+id/scrollView1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent">

        <LinearLayout   android:id="@+id/linearLayout1" 
                        android:layout_width="fill_parent" 
                        android:layout_height="fill_parent" 
                        android:orientation="vertical" 
                        android:gravity="center_horizontal">

            <ImageView  android:id="@+id/imageView1" 
                        android:layout_width="wrap_content" 
                        android:src="@drawable/icon" 
                        android:layout_height="wrap_content"></ImageView>

            <TableLayout    android:id="@+id/tableLayout1" 
                            android:layout_height="wrap_content" 
                            android:layout_width="wrap_content" 
                            android:background="@drawable/backgroundsmallobj">

                <TableRow   android:id="@+id/tableRow1" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <TextView   android:text="TITLE" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content" 
                                android:id="@+id/textView1">
                    </TextView>

                </TableRow>

                <TableRow   android:id="@+id/tableRow2" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText2" 
                                android:layout_width="wrap_content"     
                                android:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow   android:id="@+id/tableRow3" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText3" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content"
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText4" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow   android:id="@+id/tableRow5" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText"
                                android:maxLines="1" 
                                android:id="@+id/editText5" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content">
                    <EditText android:text="EditText" android:maxLines="1" android:id="@+id/editText6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeOptions="actionDone"></EditText>
                </TableRow>

                <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content">
                    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
                </TableRow>


            </TableLayout>

        </LinearLayout>

    </ScrollView>

    <FrameLayout    android:id="@+id/frameLayout1" 
                    android:layout_height="wrap_content" 
                    android:layout_alignParentBottom="true" 
                    android:layout_width="fill_parent">

        <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     ads:adUnitId="############"
                     ads:adSize="BANNER"
                     ads:loadAdOnCreate="true" />

    </FrameLayout>

</RelativeLayout>
5个回答

20

当广告覆盖时,您可以更改键盘的行为。进入AndroidManifest.xml,并在具有AdMob横幅的activity标签中添加此属性。

android:windowSoftInputMode="adjustPan"

这将防止广告跳到键盘上方并隐藏输入。相反,它们将出现在键盘后面(隐藏)。我不知道这是否违反了AdMob的政策,我只是提供解决方案。


2
在活动的清单文件中添加以下内容:android:windowSoftInputMode="stateVisible|adjustPan"。这将使键盘在弹出时不会覆盖屏幕上的输入框。请注意保留HTML标签。

1
我尝试设置android:windowSoftInputMode="adjustPan"。它会隐藏adMob横幅,但也会隐藏EdiText。所以我找到的解决方案是在键盘打开时隐藏adMob横幅。我学会了如何检测键盘是否打开这里。这是我的解决方案:
final View activityRootView = findViewById(R.id.sample_main_layout);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                    if (heightDiff > Support.dpToPx(MainActivity.this, 200)) { // if more than 200 dp, it's probably a keyboard...
                        mAdView.setVisibility(View.GONE);
                    }
                    else{
                        mAdView.setVisibility(View.VISIBLE);
                    }
                 }
            });

0

我在我的Ionic应用程序中遇到了类似的问题。当软键盘出现时,我希望广告完全隐藏,但当软键盘消失时可见。我通过在config.xml<platform name="android">标签中添加以下内容来解决这个问题:

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application/activity">
            <activity android:windowSoftInputMode="adjustPan|adjustResize" />
        </edit-config>
</platform>

0

这可能不是您的理想解决方案,但这是我为我的用户找到的最佳解决方案。当键盘显示时,它会覆盖我的 WebView 中的按钮和文本,从而导致糟糕的用户体验。为了解决这个问题,我将我的 AdView 高度设置为横幅广告的高度,在我的情况下为 50dp,并将 AdView 布局设置为在 WebView 下方。最后,我将 WebView 高度设置为 wrap_content。当键盘可见时,广告会被暂时移除,因为它没有足够的空间,当键盘隐藏时,广告会再次显示。希望这可以帮助到您。

<WebView
    android:id="@+id/webUI"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<com.google.ads.AdView android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_below="@+id/webUI" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接