最近项目搞了沉浸式导航栏,但是与软键盘弹出冲突,布局不往上面顶,折腾几番之后,网上找到个方法但是不兼容华为 部分机型,于是做了修改,测试机型有限,我手头的机型是没有问题了,于是分享出来,直接用就行了。

用法

AndroidKeyboardHeight.assistActivity(activity);

源码如下:

package com.abase.util;import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;import java.lang.reflect.Field;/*** 自适应弹出键盘* @author wangjun* @version 1.0* @date 2016/12/5*/public class AndroidKeyboardHeight {public static void assistActivity(Activity activity) {new AndroidKeyboardHeight(activity);}private View mChildOfContent;private int usableHeightPrevious;private FrameLayout.LayoutParams frameLayoutParams;private int contentHeight;private   boolean isfirst = true;private   Activity activity;private  int statusBarHeight;public static int[] wh;// 屏幕的宽和高private AndroidKeyboardHeight(Activity activity) {//获取状态栏的高度int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);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() {if (isfirst) {contentHeight = mChildOfContent.getHeight();//兼容华为等机型isfirst = false;}possiblyResizeChildOfContent();}});frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();}//重新调整跟布局的高度private void possiblyResizeChildOfContent() {int usableHeightNow = computeUsableHeight();//当前可见高度和上一次可见高度不一致 布局变动if (usableHeightNow != usableHeightPrevious) {//int usableHeightSansKeyboard2 = mChildOfContent.getHeight();//兼容华为等机型int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();int heightDifference = usableHeightSansKeyboard - usableHeightNow;System.out.println(getScreenWH(mChildOfContent.getContext())[1]-usableHeightSansKeyboard+"==="+statusBarHeight);if (heightDifference > (usableHeightSansKeyboard / 4)) {// keyboard probably just became visibleif (usableHeightSansKeyboard-getScreenWH(mChildOfContent.getContext())[1]!=statusBarHeight){//frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;} else {frameLayoutParams.height = usableHeightSansKeyboard -heightDifference;}} else {frameLayoutParams.height = contentHeight;}mChildOfContent.requestLayout();usableHeightPrevious = usableHeightNow;}}/**     * 计算mChildOfContent可见高度     ** @return     */private int computeUsableHeight() {Rect r = new Rect();mChildOfContent.getWindowVisibleDisplayFrame(r);return (r.bottom - r.top);}/*** 获取屏幕大小**            1是宽 2是高*/public int[] getScreenWH(Context context) {if (wh != null && wh[0] != 0 && wh[1] != 0) {return wh;}DisplayMetrics displayMetrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);int width = 0;int height = 0;width = displayMetrics.widthPixels;height = displayMetrics.heightPixels - getStatusBarHeight(context);// 去掉通知栏的高度int[] is = { width, height };wh = is;return is;}/*** 获取通知栏的高度** @param context* @return*/public static int getStatusBarHeight(Context context) {Class<?> c = null;Object obj = null;Field field = null;int x = 0, statusBarHeight = 0;try {c = Class.forName("com.android.internal.R$dimen");obj = c.newInstance();field = c.getField("status_bar_height");x = Integer.parseInt(field.get(obj).toString());statusBarHeight = context.getResources().getDimensionPixelSize(x);} catch (Exception e1) {e1.printStackTrace();}return statusBarHeight;}
}

android沉浸式导航栏与键盘的冲突相关推荐

  1. android 沉浸式导航栏

    <StatusBaranimated={true} //指定状态栏的变化是否应以动画形式呈现.目前支持这几种样式:backgroundColor, barStyle和hiddenhidden={ ...

  2. android 4.4 以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实现

    ImmersionBar 项目地址:gyf-dev/ImmersionBar  简介:android 4.4 以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实现,以及对 bar 的其他设置,详见 R ...

  3. Android使用沉浸式导航栏

    Android开发中,往往会有一种需求,那就是沉浸式导航栏(什么是沉浸式导航栏,这里就不做展开,自行百度).那么,怎么做到沉浸式状态栏呢,咱们接着往下看... 首先,在activity中加入 @Ove ...

  4. 微信小程序——沉浸式导航栏实现(含iphoneX适配和组件封装)

    文章目录 前言 正文 JSON navBar.js navBar.wxml navBar.wxss test1.wxml 展示效果 总结 前言 微信小程序中导航栏一般来说是默认的展示标题等等,可以做的 ...

  5. uniapp设置导航栏、沉浸式导航栏以及获取屏幕尺寸

    页面上往往会有一些需要随着屏幕的大小变化而变化的样式,这时,我们就需要获取到屏幕的宽度和高度. 当然,一个H5页面或者微信小程序页面的导航栏会有多种形态,可以带有导航栏,也可以设置为沉浸式导航栏(即导 ...

  6. uni-app实现小程序沉浸式导航栏/顶部组件占满导航栏

    uni-app实现小程序沉浸式导航栏/顶部组件占满导航栏 如果你只是想单纯的让屏幕最上方的那个组件(比如swiper)将通知栏(导航栏)填充满,那么你只需要在page.json文件中加入以下代码到你想 ...

  7. 微信小程序——沉浸式导航栏实现

    微信小程序--沉浸式导航栏实现 微信小程序中导航栏一般来说是默认的展示标题等等,可以做的样式改变仅仅能通过配置一些官方提供的属性来实现.除此之外小程序还提供了navigationStyle这个属性可以 ...

  8. APICLOUD实现沉浸式导航栏在Android和ios上的兼容

    最近在使用apicloud开发一款APP,项目没有使用安卓和ios开发人员,选择APICLOUD开发,犹豫UI设计的导航栏是沉浸式,在Android和ios上的效果是不一样的,ios上导航栏会浮在内容 ...

  9. 沉浸式导航栏html,混合开发 h5+ 沉浸式的适配

    1.需要在mainfest.json plus对象里添加 "statusbar": { "immersed": "true", " ...

最新文章

  1. 「完结」12篇文章带你逛遍主流分割网络
  2. Java使用jfreechart画饼图_jfreechart应用_例题_生成饼状图(展示,生成文件,WEB项目)_可以直接运行...
  3. ​linux 系统出现Give root password for maintenance 问题
  4. AUTHORITY-CHECK常用事务代码
  5. python requests库作用_python Requests库入门
  6. 指甲之指甲长期没有甲半月弧(二)
  7. 2021年信用卡行业发展报告
  8. 从 A/Looper: Could not create epoll instance. errno=24 错误浅谈解决各种 bug 的思路
  9. ubuntu下的snappy安装配置以及测试
  10. Python3 发票导出XML转Excel
  11. 常见的三个网络协议的区别:TCP/IP、NetBEUI、IPX/SPX
  12. FbinstTools制作多系统启动U盘(Windows+Linux)
  13. 宏基4752g linux驱动下载,宏碁笔记本及应用程序驱动下载_硬件驱动下载
  14. DFT泄漏 频谱泄漏 (FFT)
  15. 《SEM长尾搜索营销策略解密》一一2.3 一个长尾周期倒下来,还有千千万万个站起来...
  16. 【UE4】给游戏制作一个简易的小地图
  17. 阿里云国际版查看云服务器ecs实例系统日志和截图-Unirech
  18. 宇视摄像头尾线防水处理
  19. 图解LeetCode06:Z字形变换
  20. [转载]LFSR的工作原理以及LFSR在CRC上的应用

热门文章

  1. 【技术分享】如何用 TLA+ 思维为分布式算法建模
  2. RTU和DTU分别是什么?
  3. Unity AR Shadow 阴影
  4. cmpp2.0如何实现网关短信发送
  5. Android:<10>触摸切图和页面跳转的介绍
  6. Blender进行DEM数据3D制图(一)
  7. 游戏逆向-2.2VEH+软件/硬件断点实现hook
  8. AAC-LC 是什么格式?和 AAC 有什么区别?
  9. 成华区的顾连康复医院做康复治疗费用,能报销不?
  10. sparksql Error in query: resolved attribute(s)报错分析