简体   繁体   English

adMob 横幅在软键盘弹出时覆盖文本视图

[英]adMob banner covers text view when soft keyboard pops up

I have been having troubles keeping an adMob banner from rolling up along the soft keyboard and covering my text views....我一直无法保持 adMob 横幅沿软键盘滚动并覆盖我的文本视图....

This is the link to what I am trying to achieve in Android: http://bit.ly/myfoodcalc这是我试图在 Android 中实现的链接: http://bit.ly/myfoodcalc

I know my layout is a little bit complex, too many nested views (maybe), etc.我知道我的布局有点复杂,嵌套视图太多(也许)等等。

I have tryed changing things on the Manifest without success.我曾尝试在 Manifest 上更改内容但没有成功。

For you to understand it better here is son pseudo XML (and the full XML later on the post)...为了让您更好地理解它,这里是儿子伪 XML (以及后面的完整 XML)...

 <ScrollView>
      <Linear layout>
            <ImageView>
            <TableView>
                  <Several rows with a Edit Text widget>
            </TableView>
            </ImageView>
      </LinearLayout>
  </ScrollView>

  <FrameLayout>
       <ImageView>
  </FrameLayout>

An here is my complete XML这是我完整的 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>

You can change how the keyboard behaves when it appears over the Ad.您可以更改键盘出现在广告上时的行为方式。 Go into your AndroidManifest.xml and add this attribute in the activity tag with the AdMob banner. Go 到您的 AndroidManifest.xml 并在活动标签中添加此属性,并带有 AdMob 横幅。

android:windowSoftInputMode="adjustPan"

This will prevent the ads from jumping above the keyboard and hiding the input.这将防止广告跳到键盘上方并隐藏输入。 Instead, they will appear behind the keyboard (hidden).相反,它们将出现在键盘后面(隐藏)。 I don't know if this is against AdMob policy, I am just providing a solution.我不知道这是否违反 AdMob 政策,我只是提供一个解决方案。

I tried setting android:windowSoftInputMode="adjustPan".我尝试设置 android:windowSoftInputMode="adjustPan"。 it hides the adMob banner but also the EdiText.它隐藏了 adMob 横幅以及EdiText So the solution I found is to hide the adMob banner when the keyboard opens.所以我找到的解决方案是在键盘打开时隐藏 adMob 横幅。 I learned how to detect if the keyboard opens here .我学会了如何检测键盘是否在此处打开。 and here is my solution:这是我的解决方案:

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);
                    }
                 }
            });

add this in your Manifest in activity android:windowSoftInputMode="stateVisible|adjustPan"将此添加到您的清单中的活动 android:windowSoftInputMode="stateVisible|adjustPan"

This may not be the ideal solution for you but it is what I found to be best for my users.这对您来说可能不是理想的解决方案,但我发现它对我的用户来说是最好的。 When the keyboard was shown it was covering buttons and text in my WebView making for a bad user experience.当显示键盘时,它覆盖了我的 WebView 中的按钮和文本,从而导致糟糕的用户体验。 To fix this I set my AdView height to the height of the banner ad, in my case 50dp and set the AdView layout to below to the WebView.为了解决这个问题,我将 AdView 高度设置为横幅广告的高度,在我的情况下为 50dp,并将 AdView 布局设置为 WebView 下方。 Finally I set the WebView height to wrap_content.最后,我将 WebView 高度设置为 wrap_content。 When the keyboard is visible the ad is temporary removed because it runs out of space, when the keyboard is hidden the ad is shown again.当键盘可见时,广告会因为空间不足而暂时移除,当键盘隐藏时,广告会再次显示。 Hope this helps.希望这可以帮助。

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM