解决输入法遮挡布局和华为手机底部虚拟按键遮挡底部界面的方法

news/2024/11/25 9:40:08/

先上最终的代码:

public class AndroidWorkaround {public static void assistActivity(View content) {new AndroidWorkaround(content);}private View mChildOfContent;private int usableHeightPrevious;private ViewGroup.LayoutParams frameLayoutParams;private AndroidWorkaround(View content) {mChildOfContent = content;mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {public void onGlobalLayout() {possiblyResizeChildOfContent();}});frameLayoutParams = mChildOfContent.getLayoutParams();}private void possiblyResizeChildOfContent() {int usableHeightNow = computeUsableHeight();if (usableHeightNow != usableHeightPrevious) {frameLayoutParams.height = usableHeightNow;mChildOfContent.requestLayout();usableHeightPrevious = usableHeightNow;}}private int computeUsableHeight() {Rect r = new Rect();mChildOfContent.getWindowVisibleDisplayFrame(r);return (r.bottom);}public static boolean checkDeviceHasNavigationBar(Context context) {boolean hasNavigationBar = false;Resources rs = context.getResources();int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");if (id > 0) {hasNavigationBar = rs.getBoolean(id);}try {Class systemPropertiesClass = Class.forName("android.os.SystemProperties");Method m = systemPropertiesClass.getMethod("get", String.class);String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");if ("1".equals(navBarOverride)) {hasNavigationBar = false;} else if ("0".equals(navBarOverride)) {hasNavigationBar = true;}} catch (Exception e) {}return hasNavigationBar;}}
 
注意点:这里构造方法的参数是当前你写的布局view
-----------------------------------------------------------------------------------------------
PS:
1.单单解决输入法挡住软键盘
public class AndroidBug5497Workaround {// For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity(Activity activity) {new AndroidBug5497Workaround(activity);}private Activity activity;private View mChildOfContent;private int usableHeightPrevious;private FrameLayout.LayoutParams frameLayoutParams;private AndroidBug5497Workaround(Activity activity) {this.activity = activity;FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);mChildOfContent = content.getChildAt(0);mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {public void onGlobalLayout() {possiblyResizeChildOfContent();}});frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();}private void possiblyResizeChildOfContent() {int usableHeightNow = computeUsableHeight();if (usableHeightNow != usableHeightPrevious) {int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();//这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {Rect frame = new Rect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;usableHeightSansKeyboard -= statusBarHeight;}int heightDifference = usableHeightSansKeyboard - usableHeightNow;if (heightDifference > (usableHeightSansKeyboard / 4)) {// keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;} else {// keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;}mChildOfContent.requestLayout();usableHeightPrevious = usableHeightNow;}}private int computeUsableHeight() {Rect frame = new Rect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;Rect r = new Rect();mChildOfContent.getWindowVisibleDisplayFrame(r);//这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {return (r.bottom - r.top) + statusBarHeight;}return (r.bottom - r.top);}}
2.单单解决华为手机虚拟按键挡住底部界面

public class AndroidBug54971Workaround {// For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    /**
     * 关联要监听的视图
     *
     * @param viewObserving
     */
    public static void assistActivity(View viewObserving) {new AndroidBug54971Workaround(viewObserving);}private View mViewObserved;//被监听的视图
    private int usableHeightPrevious;//视图变化前的可用高度
    private ViewGroup.LayoutParams frameLayoutParams;private AndroidBug54971Workaround(View viewObserving) {mViewObserved = viewObserving;//给View添加全局的布局监听器
        mViewObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {public void onGlobalLayout() {resetLayoutByUsableHeight(computeUsableHeight());}});frameLayoutParams = mViewObserved.getLayoutParams();}private void resetLayoutByUsableHeight(int usableHeightNow) {//比较布局变化前后的View的可用高度
        if (usableHeightNow != usableHeightPrevious) {//如果两次高度不一致
            //将当前的View的可用高度设置成View的实际高度
            frameLayoutParams.height = usableHeightNow;mViewObserved.requestLayout();//请求重新布局
            usableHeightPrevious = usableHeightNow;}}/**
     * 计算视图可视高度
     *
     * @return
     */
    private int computeUsableHeight() {Rect r = new Rect();mViewObserved.getWindowVisibleDisplayFrame(r);return (r.bottom - r.top);}
}
注意点:setContentView(R.layout.content_frame); 
AndroidBug54971Workaround.assistActivity(findViewById(android.R.id.content));



http://www.ppmy.cn/news/441175.html

相关文章

台湾源码学习1

D:\git\dialog\6.0.10.511\projects\target_apps\ble_examples\mCube_mc36xx_data_notifcation\src\config\user_callback_config.h 和node的这个对比 右边台湾基本一样&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;只是多了下面的3个回调函数 修改代码编译 …

全球充电最快手机:5分钟回血50%;华为未发布新手机 | MWC 2022

金磊 假装发自 巴塞罗那量子位 | 公众号 QbitAI 一年一度的全球手机行业盛宴——世界移动通信大会 &#xff08;MWC&#xff09;&#xff0c;已经在巴塞罗那正式拉开序幕&#xff08;2月28日-3月3日&#xff09;。 MWC是手机通讯行业最重要的展会&#xff0c;每年国内外手机厂商…

Android报错:Using WebView from more than one process at once with the same data directory is not...

安卓项目中加载网页使用腾讯的TBS&#xff0c;X5内核一直还好用&#xff0c;发现部分用户手机出现了偶尔闪退的情况。 研究后发现报错信息&#xff1a; Using WebView from more than one process at once with the same data directory is not supported. 完整信息&#xff…

华为手机版本互刷

[前言] 1、互刷是指的官方ROM&#xff0c;比如国行刷成国际版享受全面的Google服务&#xff0c;或者国际版刷成国行更适合国内使用 2、菊花的服务站是拒绝的&#xff0c;3月的时候我去售后&#xff0c;攻城狮直言硬刷会成砖&#xff0c;成功吓退了我 3、刷机难免有失手&#xf…

性能下降47%!华为、荣耀四款手机跑分作弊实锤,3DMark宣布榜单除名

楚匆 发自 凹非寺量子位 出品 | 公众号 QbitAI 不知道华为堵不堵心。 在Mate 20正式发售前这最不该掉链子的一个多月&#xff0c;华为绊倒了两次。 半个月前的AI拍照“虚假宣传”一事还未平静&#xff0c;新的公关危机再次降临&#xff0c;而这次的影响似乎更严重一些&#xff…

华为路由器后台登录协议实现

实际使用路由器为荣耀路由Pro 下面所述过程&#xff0c;很大部分参考了下面两篇博客https://blog.csdn.net/Touale/article/details/113571862https://www.ruterfu.com/2021/04/19/20210419-login-by-codes-huawei-router-WS5200/ 其中第一篇博客稍显不好&#xff0c;只是给出…

Surface Pro 5 开启华为多屏协同

程序有问题时不要担心。如果所有东西都没问题&#xff0c;你就失业了。 参考的教程在此&#xff1a; 傻瓜式操作5分钟搞定非华为电脑一碰传 网上好多教程帖以及资源都被删掉了&#xff0c;也不知道是为什么&#xff0c;还以为真的得支持一个华为电脑了&#xff0c;结果在花粉俱…

HTML5期末考核大作业:华为官网 ( 2页带轮播图)

⛵ 源码获取 文末联系 ✈ Web前端开发技术 描述 网页设计题材&#xff0c;DIVCSS 布局制作,HTMLCSS网页设计期末课程大作业 |公司官网网站 | 企业官网 | 酒店官网 | 等网站的设计与制 | HTML期末大学生网页设计作业&#xff0c;Web大学生网页 HTML&#xff1a;结构 CSS&#x…