打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在Android上打开键盘时如何隐藏广告横幅

我将MoPubView用于我的应用中的广告.但是,当我打开键盘时,该横幅会升起并关闭小部件.

这是来自布局的代码:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@ id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/material_bg">    <ScrollView        android:id="@ id/scroll"        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:paddingBottom="@dimen/activity_horizontal_margin">            ...Content here        </LinearLayout>    </ScrollView>    <com.mopub.mobileads.MoPubView        android:id="@ id/adview"        android:layout_width="fill_parent"        android:layout_height="@dimen/mopub_height"        android:layout_gravity="bottom"/></FrameLayout>

此布局的此方案:


如何隐藏此横幅或将其固定在底部?

解决方法:

创建一个类来处理键盘检测,

import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver;import java.util.LinkedList;import java.util.List;public class SoftKeyboardStateWatcher implements ViewTreeObserver.OnGlobalLayoutListener {    public interface SoftKeyboardStateListener {        void onSoftKeyboardOpened(int keyboardHeightInPx);        void onSoftKeyboardClosed();    }    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();    private final View activityRootView;    private int        lastSoftKeyboardHeightInPx;    private boolean    isSoftKeyboardOpened;    public SoftKeyboardStateWatcher(View activityRootView) {        this(activityRootView, false);    }    public SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) {        this.activityRootView     = activityRootView;        this.isSoftKeyboardOpened = isSoftKeyboardOpened;        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);    }    @Override    public void onGlobalLayout() {        final Rect r = new Rect();        //r will be populated with the coordinates of your view that area still visible.        activityRootView.getWindowVisibleDisplayFrame(r);        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...            isSoftKeyboardOpened = true;            notifyOnSoftKeyboardOpened(heightDiff);        } else if (isSoftKeyboardOpened && heightDiff < 100) {            isSoftKeyboardOpened = false;            notifyOnSoftKeyboardClosed();        }    }    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {        this.isSoftKeyboardOpened = isSoftKeyboardOpened;    }    public boolean isSoftKeyboardOpened() {        return isSoftKeyboardOpened;    }    /**     * Default value is zero {@code 0}.     *     * @return last saved keyboard height in px     */    public int getLastSoftKeyboardHeightInPx() {        return lastSoftKeyboardHeightInPx;    }    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.add(listener);    }    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.remove(listener);    }    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardOpened(keyboardHeightInPx);            }        }    }    private void notifyOnSoftKeyboardClosed() {        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardClosed();            }        }    }}

在您的AdActivity的onCreate方法中,插入以下行:

final SoftKeyboardStateWatcher softKeyboardStateWatcher = new SoftKeyboardStateWatcher(findViewById(R.id.container);        // Add listener        softKeyboardStateWatcher.addSoftKeyboardStateListener(new SoftKeyboardStateWatcher.SoftKeyboardStateListener() {                @Override                public void onSoftKeyboardOpened(int keyboardHeightInPx) {                }                @Override                public void onSoftKeyboardClosed() {                }            });        // then just handle callbacks
来源:https://www.icode9.com/content-4-566451.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Android 监听键盘状态 获取键盘高度
Onclick listener for TextView (Android forum at Coderanch)
手把手教你快速搞定京东APP设置页面布局
Android的UI组件复选框控件CheckBox | 第三极 | 移动开发者
Android开发之监听软键盘状态(弹出收回)
TabHost通过手势切换Activity,滑动效果
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服