android 虚拟键盘遮挡布局 顶起布局

在 android 的 XML 中设置 fitsSystemWindows 属性的分析:

fitsSystemWindows 只作用在 sdk>=19 的系统上就是高于 4.4 的系统

android:fitsSystemWindows="true"

这个属性可以给任何 view 设置,只要设置了这个属性此 view 的所有 padding 属性失效,而且只有在设置了透明状态栏 (StatusBar) 或者导航栏 (NavigationBar) 时此属性才会生效。

1. 当设置了透明状态栏(StatusBar)时:

当为此 activity 设置了:

<item name="android:windowTranslucentStatus">true</item>

或者

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
	getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

同时,所有设置了 android:fitsSystemWindows=”true” 属性的 view 会自动添加一个值等于状态栏高度的 paddingTop

2. 当设置了透明导航栏(NavigationBar)时:

当为此 activity 设置了:

<item name="android:windowTranslucentNavigation">true</item>

或者

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
	getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

同时,所有设置了 android:fitsSystemWindows=”true” 属性的 view 会自动添加一个值等于导航栏高度的 paddingBottom

3. 附加一个获取 StatusBar 的和一个获取 NavigationBar 高度的代码:

public float getStatusBarHeight() { // 返回值就是状态栏的高度,得到的值是像素
    float result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimension(resourceId);
    }
    return result;
}

public float getNavigationBarHeight() { // 返回值就是导航栏的高度,得到的值是像素
    float result = 0;
    int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimension(resourceId);
    }
    return result;
}

其他

1、有时候,当软键盘弹出时,会将我们的 view 顶上去,可以通过设置如下属性进行避免

android:windowSoftInputMode="adjustResize|stateHidden"

2、待补充。。。

------至所有正在努力奋斗的程序猿们!加油!!
有码走遍天下 无码寸步难行
1024 - 梦想,永不止步!
爱编程 不爱Bug
爱加班 不爱黑眼圈
固执 但不偏执
疯狂 但不疯癫
生活里的菜鸟
工作中的大神
身怀宝藏,一心憧憬星辰大海
追求极致,目标始于高山之巅
一群怀揣好奇,梦想改变世界的孩子
一群追日逐浪,正在改变世界的极客
你们用最美的语言,诠释着科技的力量
你们用极速的创新,引领着时代的变迁

——乐于分享,共同进步,欢迎补充
——Any comments greatly appreciated
——诚心欢迎各位交流讨论!QQ:1138517609
——CSDN:https://blog.csdn.net/u011489043
——简书:https://www.jianshu.com/u/4968682d58d1
——GitHub:https://github.com/selfconzrr

发布了79 篇原创文章 · 获赞 207 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/u011489043/article/details/87889549
今日推荐