之前的Android沉浸式状态栏实现并没有考虑软键盘的影响,接下来的内容将会针对这个问题给出解决方式,先看一下效果图

这个是一个留言板的效果图:

即弹出软键盘的时候并不会导致整个布局上移。

详细怎样实现?依照下面步骤进行设置:

1、布局文件里声明例如以下

  1. <activity
  2. android:name="com.storm.durian.activity.LeaveMessageDetailsActivity"
  3. android:screenOrientation="portrait"
  4. />

2、这里重写了RelativeLayout。并重写了fitSystemWindows,这个是最最关键的。

  1. /**
  2. * 自适应布局
  3. */
  4. public class FitsSystemWindowsLayout extends RelativeLayout {
  5. private static final String TAG = FitsSystemWindowsLayout.class.getSimpleName();
  6. private SoftKeyBoardStateListener softKeyBoardStateListener;
  7.  
  8. public FitsSystemWindowsLayout(Context context) {
  9. super(context);
  10. }
  11.  
  12. public FitsSystemWindowsLayout(Context context, AttributeSet attrs) {
  13. super(context, attrs);
  14. }
  15.  
  16. public FitsSystemWindowsLayout(Context context, AttributeSet attrs, int defStyle) {
  17. super(context, attrs, defStyle);
  18. }
  19.  
  20. @Override
  21. protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
  22.  
  23. dispatchListenerLow(heightMeasureSpec);
  24. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  25. }
  26.  
  27. /**
  28. * 处理低版本号键盘弹出
  29. *
  30. * @param heightMeasureSpec 高度
  31. */
  32. private void dispatchListenerLow(int heightMeasureSpec) {
  33. if (softKeyBoardStateListener == null || Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  34. return;
  35. }
  36. int oldSpec = getMeasuredHeight();
  37.  
  38. if (oldSpec <= 0) {
  39. return;
  40. }
  41. int newSpec = MeasureSpec.getSize(heightMeasureSpec);
  42. int offset = oldSpec - newSpec;
  43. if (offset > 100) {
  44. LogHelper.i(TAG, "键盘打开");
  45. softKeyBoardStateListener.onSoftKeyBoardStateChange(true);
  46. } else if (offset < -100) {
  47. LogHelper.i(TAG, "键盘关闭");
  48. softKeyBoardStateListener.onSoftKeyBoardStateChange(false);
  49. }
  50. }
  51.  
  52. @Override
  53. protected boolean fitSystemWindows(Rect insets) {
  54. dispatchListener(insets);
  55. insets.top = 0;
  56. return super.fitSystemWindows(insets);
  57. }
  58.  
  59. /**
  60. * 分发监听
  61. *
  62. * @param insets
  63. */
  64. private void dispatchListener(Rect insets) {
  65. if (softKeyBoardStateListener == null || android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  66. return;
  67. }
  68. if (insets.top != 0 && insets.bottom != 0) {
  69. LogHelper.i(TAG, "键盘打开");
  70. softKeyBoardStateListener.onSoftKeyBoardStateChange(true);
  71. } else {
  72. LogHelper.i(TAG, "键盘关闭");
  73. softKeyBoardStateListener.onSoftKeyBoardStateChange(false);
  74. }
  75.  
  76. }
  77.  
  78. /**
  79. * 设置软键盘监听事件
  80. *
  81. * @param softKeyBoardStateListener
  82. */
  83. public void setSoftKeyBoardListener(SoftKeyBoardStateListener softKeyBoardStateListener) {
  84. this.softKeyBoardStateListener = softKeyBoardStateListener;
  85. }
  86.  
  87. public interface SoftKeyBoardStateListener {
  88.  
  89. public void onSoftKeyBoardStateChange(boolean isOpen);
  90.  
  91. }
  92. }

尽管以上布局也提供了键盘打开或者关闭的回调,可是在某些低版本号手机上还是支持的不太好,须要这个回调的能够将此回调作为当中一个方法。可是不要过分依赖

3、布局文件

使用com.storm.durian.view.FitsSystemWindowsLayout包裹整个布局,并设置android:fitsSystemWindows=“true”

  1. <?xml version="1.0" encoding="utf-8"?
  2.  
  3. >
  4. <com.storm.durian.view.FitsSystemWindowsLayout android:id="@+id/leave_message_layout"
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:background="@color/ececec"
  9. android:fitsSystemWindows="true">
  10.  
  11. <include
  12. android:id="@+id/leave_message_title"
  13. layout="@layout/common_back"/>
  14.  
  15. <LinearLayout
  16. android:id="@+id/ll_leave_message_input"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentBottom="true"
  20. android:background="@drawable/leave_message_bottom_bg"
  21. android:orientation="horizontal">
  22.  
  23. <EditText
  24. android:id="@+id/et_leave_message"
  25. android:layout_width="0dp"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="3"
  28. android:background="@drawable/et_leave_message_bg"
  29. android:hint="亲,给我留言我会回复你哒~"
  30. android:maxLines="4"
  31. android:textColor="@color/_3e363d"
  32. android:textSize="@dimen/text_size_small"/>
  33.  
  34. <Button
  35. android:id="@+id/btn_leave_message_send"
  36. android:layout_width="0dp"
  37. android:layout_height="wrap_content"
  38. android:layout_marginLeft="10dp"
  39. android:layout_weight="1"
  40. android:background="@drawable/btn_leave_message_send_selector"
  41. android:singleLine="true"
  42. android:text="发送"
  43. android:textColor="@color/_3e363d"
  44. android:textSize="@dimen/text_size_middle"/>
  45. </LinearLayout>
  46.  
  47. <ListView
  48. android:id="@+id/lv_leave_message"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content"
  51. android:layout_above="@id/ll_leave_message_input"
  52. android:layout_below="@id/leave_message_title"
  53. android:cacheColorHint="#00000000"
  54. android:divider="@color/dbdbdb"
  55. android:dividerHeight="1dp"
  56. android:fadingEdge="none"/>
  57.  
  58. <ViewStub
  59. android:id="@+id/activity_leave_message_loading_stub"
  60. android:layout_width="match_parent"
  61. android:layout_height="match_parent"
  62. android:layout_below="@id/leave_message_title"
  63. android:inflatedId="@+id/activity_leave_message_loading_subTree"
  64. android:layout="@layout/common_loading"/>
  65.  
  66. <ViewStub
  67. android:id="@+id/leave_message_empty_stub"
  68. android:layout_width="match_parent"
  69. android:layout_height="match_parent"
  70. android:layout_above="@id/ll_leave_message_input"
  71. android:layout_below="@id/leave_message_title"
  72. android:inflatedId="@+id/leave_message_empty_subTree"
  73. android:layout="@layout/common_tips_layout"/>
  74.  
  75. <ViewStub
  76. android:id="@+id/leave_message_net_error_stub"
  77. android:layout_width="match_parent"
  78. android:layout_height="match_parent"
  79. android:layout_below="@id/leave_message_title"
  80. android:inflatedId="@+id/leave_message_net_error_subTree"
  81. android:layout="@layout/common_net_error_layout"/>
  82.  
  83. </com.storm.durian.view.FitsSystemWindowsLayout>

4、Activity中处理

设置setImmerseLayout(findViewById(R.id.leave_message_title));

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_leave_detail_message);
  5. setImmerseLayout(findViewById(R.id.leave_message_title));
  6. logic = LeaveMessageLogic.getInstance(getApplicationContext());
  7. mHandler = new MyHandler(this);
  8. initView();
  9. }

再看BaseActivity中的setImmerseLayout方法

版本号要求仍然是4.4以上

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. isStart = true;
  5. ShareSDK.initSDK(this);
  6. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  7. //透明状态栏
  8. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  9. //透明导航栏
  10. // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  11. statusBarHeight = ScreenUtil.getStatusBarHeight(this);
  12. }
  13. ScreenManager.getScreenManager().pushActivity(this);
  14. }
  1. protected void setImmerseLayout(View view) {
  2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  3. Window window = getWindow();
  4. window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
  5. WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  6. // window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  7.  
  8. int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
  9. view.setPadding(0, statusBarHeight, 0, 0);
  10. }
  11. }

OK。须要的朋友们能够自己尝试一下!

有空的话会将这个沉浸式的留言板的功能写一下分享出来!

【Android实战】Android沉浸式状态栏实现(下)的更多相关文章

  1. Android中的沉浸式状态栏效果

    无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...

  2. Android 4.4沉浸式状态栏的实现

    要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint) public clas ...

  3. Android Studio 关于沉浸式状态栏

    values-v19/style.xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoAction ...

  4. [置顶] Xamarin android沉浸式状态栏

    虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...

  5. 81.Android之沉浸式状态栏攻略

    转载:http://blog.csdn.net/lmj623565791/article/details/48649563/ 一.概述 近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下:本篇博客 ...

  6. android沉浸式状态栏设置(4.4以上版本)

    其实设置比较简单,我用了小米和htc的几款机型都可以用. 主要代码就是这个(注意要在Activity的setContentView之前调用才行) /** * 开启沉浸式状态栏 * */ public ...

  7. Android 沉浸式状态栏攻略 让你的状态栏变色吧

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48649563: 本文出自:[张鸿洋的博客] 一.概述 近期注意到QQ新版使用了 ...

  8. Android 沉浸式状态栏完美解决方案

    现在搜索Android 沉浸式状态栏,真的是一堆一堆,写的特别多,但是真正用的舒服的真没有,在这里自己整理一下开发记录 注意,在使用这个步骤过程之前,请把之前设置的代码注释一下 把布局带有androi ...

  9. android -------- 沉浸式状态栏和沉浸式导航栏(ImmersionBar)

    android 4.4以上沉浸式状态栏和沉浸式导航栏管理,包括状态栏字体颜色,适用于Activity.Fragment.DialogFragment.Dialog,并且适配刘海屏,适配软键盘弹出等问题 ...

随机推荐

  1. bzoj2438: [中山市选2011]杀人游戏(强联通+特判)

    2438: [中山市选2011]杀人游戏 题目:传送门 简要题意: 给出n个点,m条有向边,进行最少的访问并且可以便利(n-1)个点,求这个方案成功的概率 题解: 一道非常好的题目! 题目要知道最大的 ...

  2. LOJ #109. 并查集

    内存限制:256 MiB时间限制:2000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论 1 测试数据   题目描述 这是一道模板题. 维护一个 nnn 点 ...

  3. Angular2/Ionic2集成Promact/md2.md

    最近想找一套比较完整的基于Material风格的Angular2的控件库,有两个选择一个是Angular官方的Material2,但是这套库的DatePicker控件目前只能支持年月日,不支持时分秒, ...

  4. ubuntu 18.04网卡命名规则改回传统的ethx

    自15版本开始网卡命名规则就不叫eth0了.而是用可预期网络接口设备名称的命名规则,比如网卡名为enp3s0 . 如果想要变回ethx也是可以的,参考以下步骤: 1.编辑/etc/default/gr ...

  5. Java ——代理模式[转发]

    1.  简介 代理模式(Proxy Pattern)是GoF 23种Java常用设计模式之一.代理模式的定义:Provide a surrogate or placeholder for anothe ...

  6. vue中的三级联动

    1.template里面的内容 2.js里面的内容 3.函数怎么写? 这是一个省市区的三级联动,首先你要传递中国的id,这样才能获取到所有的省份,所以在vue的项目中,我需要发一次进页面就请求(来得到 ...

  7. linux傻瓜式安装lnmp

    一.百度 https://lnmp.org/install.html 二.点击 <安装> 三.登录 linux cd /usr/local/ wget -c http://soft.vps ...

  8. last-child到底怎么用

    今天工作时候遇到的坑, 看来还是css基础不够扎实,特此记录一下, <div> <p>1</p> <p>2</p> <p>3&l ...

  9. HDU 2253 Longest Common Subsequence Again

    其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string. ...

  10. Unity shader 代码高亮+提示

    Shader Unity Support This is Unity CG Shaders Support. It has code completion support and uses C/C++ ...