android沉浸式导航栏与键盘的冲突
最近项目搞了沉浸式导航栏,但是与软键盘弹出冲突,布局不往上面顶,折腾几番之后,网上找到个方法但是不兼容华为 部分机型,于是做了修改,测试机型有限,我手头的机型是没有问题了,于是分享出来,直接用就行了。
用法
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沉浸式导航栏与键盘的冲突相关推荐
- android 沉浸式导航栏
<StatusBaranimated={true} //指定状态栏的变化是否应以动画形式呈现.目前支持这几种样式:backgroundColor, barStyle和hiddenhidden={ ...
- android 4.4 以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实现
ImmersionBar 项目地址:gyf-dev/ImmersionBar 简介:android 4.4 以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实现,以及对 bar 的其他设置,详见 R ...
- Android使用沉浸式导航栏
Android开发中,往往会有一种需求,那就是沉浸式导航栏(什么是沉浸式导航栏,这里就不做展开,自行百度).那么,怎么做到沉浸式状态栏呢,咱们接着往下看... 首先,在activity中加入 @Ove ...
- 微信小程序——沉浸式导航栏实现(含iphoneX适配和组件封装)
文章目录 前言 正文 JSON navBar.js navBar.wxml navBar.wxss test1.wxml 展示效果 总结 前言 微信小程序中导航栏一般来说是默认的展示标题等等,可以做的 ...
- uniapp设置导航栏、沉浸式导航栏以及获取屏幕尺寸
页面上往往会有一些需要随着屏幕的大小变化而变化的样式,这时,我们就需要获取到屏幕的宽度和高度. 当然,一个H5页面或者微信小程序页面的导航栏会有多种形态,可以带有导航栏,也可以设置为沉浸式导航栏(即导 ...
- uni-app实现小程序沉浸式导航栏/顶部组件占满导航栏
uni-app实现小程序沉浸式导航栏/顶部组件占满导航栏 如果你只是想单纯的让屏幕最上方的那个组件(比如swiper)将通知栏(导航栏)填充满,那么你只需要在page.json文件中加入以下代码到你想 ...
- 微信小程序——沉浸式导航栏实现
微信小程序--沉浸式导航栏实现 微信小程序中导航栏一般来说是默认的展示标题等等,可以做的样式改变仅仅能通过配置一些官方提供的属性来实现.除此之外小程序还提供了navigationStyle这个属性可以 ...
- APICLOUD实现沉浸式导航栏在Android和ios上的兼容
最近在使用apicloud开发一款APP,项目没有使用安卓和ios开发人员,选择APICLOUD开发,犹豫UI设计的导航栏是沉浸式,在Android和ios上的效果是不一样的,ios上导航栏会浮在内容 ...
- 沉浸式导航栏html,混合开发 h5+ 沉浸式的适配
1.需要在mainfest.json plus对象里添加 "statusbar": { "immersed": "true", " ...
最新文章
- 不仅仅是写代码,而是完成作品
- erhai系统使用_web
- SQLite自增关键字报错(near “AUTO_INCREMENT“: syntax error)
- Hibernate中的JPA 2.1条件删除/更新和临时表
- postscript怎么打开_怎么把在学习中用的Adobe PDF文件转换成Microsoft office Word
- 1470. 重新排列数组
- Java中Math对象的属性与方法
- java filter 注解_JAVA 之 Filter
- 单位换算:秒(s)/毫秒(ms)/微秒(us)/纳秒(ns)/皮秒(ps)之间密尔(mil)/毫米(mil)/英尺(inch)之间
- 常见文件扩展名和它们的说明(转)
- 徒步西藏旅游攻略之岗仁波齐神山之行
- java容器~面试知识点整理
- 什么是服务熔断,什么是服务降级?
- Java开发自学教程!kafka配置文件参数说明
- DCC:Deep continuous clustering
- 32位程序和64位程序
- String Ioc简介
- Codeforces Global Round 15 ABCD
- 威尔特拉斯定理_数学大师启示录维尔斯特拉斯.pdf
- arduino编码器计数_ARDUINO旋转编码器
热门文章
- LTspice软件电源设置
- 模仿酷狗7(Kugou7)界面源码
- ROS2机器人应用简明教程01文档
- 6脚数码管驱动方式(7脚类似)
- python的rich终端显示
- 画涂的软件 android,涂鸦画画精灵APP
- 文法G[E]: E→E+T|T ,T→T*F|F ,F→i|(E) 求FIRST集,FOLLOW集
- Anaconda + Pycharm + Pytorch 一套太极组合拳安装教程
- 游戏代码-LOL卡萨丁斩杀伤害计算器
- 大牛手把手教你!如何化身BAT面试收割机?附答案