欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

当界面全屏显示的时候 软键盘弹出的时候scrollview失去作用 不能上滑到底部

程序员文章站 2022-04-21 17:50:40
...

        Android中有个Bug,在设置getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);为全屏状态下点击输入框显示键盘时,界面并不会重新渲染调整位置,android:windowSoftInputMode="adjustResize"在FullScreen下没有作用。

       找了很久才找到解决方法,该方法是在Activity onCreate时通过ViewTreeObserver注册GlobalLayoutListener监听,当全局布局改变时会触发该监听。下面是代码部分:

  1. package com.maituo.sdk.util;
  2. import android.app.Activity;
  3. import android.graphics.Rect;
  4. import android.view.View;
  5. import android.view.ViewGroup.LayoutParams;
  6. import android.view.ViewTreeObserver;
  7. import android.widget.FrameLayout;
  8. public class AndroidBug5497Workaround {
  9. // For more information, see https://code.google.com/p/android/issues/detail?id=5497
  10. // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
  11. // public static void assistActivity (Activity activity) {
  12. // new AndroidBug5497Workaround(activity);
  13. // }
  14. private View mContent;
  15. private int usableHeightPrevious;
  16. private LayoutParams layoutParams;
  17. public static void assistView(View v){
  18. new AndroidBug5497Workaround(v);
  19. }
  20. public static void assistActivity (Activity activity) {
  21. new AndroidBug5497Workaround(activity);
  22. }
  23. private AndroidBug5497Workaround(Activity activity){
  24. FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
  25. mContent = content.getChildAt(0);
  26. addGlobalLayoutListener(mContent);
  27. }
  28. //有时通过Activity获取view并不能满足,所以我加了直接传view的一个构造方法满足用到Fragment的情况
  29. private AndroidBug5497Workaround(View v) {
  30. //FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
  31. // mChildOfContent = content.getChildAt(0);
  32. addGlobalLayoutListener(v);
  33. }
  34. private void addGlobalLayoutListener(View v){
  35. mContent = v;
  36. mContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  37. public void onGlobalLayout() {
  38. possiblyResizeChildOfContent();
  39. }
  40. });
  41. // LayoutParams放在这里有可能得到的为空
  42. // LayoutParams = mContent.getLayoutParams();
  43. }
  44. private void possiblyResizeChildOfContent() {
  45. int usableHeightNow = computeUsableHeight();
  46. if (usableHeightNow != usableHeightPrevious) {
  47. layoutParams = mContent.getLayoutParams();
  48. int usableHeightSansKeyboard = mContent.getRootView().getHeight();
  49. int heightDifference = usableHeightSansKeyboard - usableHeightNow;
  50. if (heightDifference > (usableHeightSansKeyboard/4)) {
  51. // keyboard probably just became visible
  52. layoutParams.height = usableHeightSansKeyboard - heightDifference;
  53. } else {
  54. // keyboard probably just became hidden
  55. layoutParams.height = usableHeightSansKeyboard;
  56. }
  57. mContent.requestLayout();
  58. usableHeightPrevious = usableHeightNow;
  59. }
  60. }
  61. private int computeUsableHeight() {
  62. Rect r = new Rect();
  63. mContent.getWindowVisibleDisplayFrame(r);
  64. return (r.bottom - r.top);
  65. }
  66. }

只要调用AndroidBug5497Workaround.assistActivity(activity)或AndroidBug5497Workaround.assistView(view)就可以了


来自https://blog.csdn.net/bdemq/article/details/46663153
相关标签: EditText