Android How to adjust layout in Full Screen Mode when softkeyboard is visible

前端 未结 25 1780
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

25条回答
  •  花落未央
    2020-11-22 04:36

    Today not working adjustResize on full screen issue is actual for android sdk.

    From answers i found:
    the solution - but solution has this showing on picture issue :

    Than i found the solution and remove the one unnecessary action:

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    

    So, see my fixed solution code on Kotlin:

    class AndroidBug5497Workaround constructor(val activity: Activity) {
    
        private val content = activity.findViewById(android.R.id.content) as FrameLayout
    
        private val mChildOfContent = content.getChildAt(0)
        private var usableHeightPrevious: Int = 0
        private val contentContainer = activity.findViewById(android.R.id.content) as ViewGroup
        private val rootView = contentContainer.getChildAt(0)
        private val rootViewLayout = rootView.layoutParams as FrameLayout.LayoutParams
    
        private val listener = {
            possiblyResizeChildOfContent()
        }
    
        fun addListener() {
            mChildOfContent.apply {
                viewTreeObserver.addOnGlobalLayoutListener(listener)
    
            }
        }
    
        fun removeListener() {
            mChildOfContent.apply {
                viewTreeObserver.removeOnGlobalLayoutListener(listener)
            }
        }
    
        private fun possiblyResizeChildOfContent() {
            val contentAreaOfWindowBounds = Rect()
            mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds)
            val usableHeightNow = contentAreaOfWindowBounds.height()
    
            if (usableHeightNow != usableHeightPrevious) {
                rootViewLayout.height = usableHeightNow
                rootView.layout(contentAreaOfWindowBounds.left,
                        contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
                mChildOfContent.requestLayout()
                usableHeightPrevious = usableHeightNow
            }
        }
    }
    

    My bug fixing implement code:

     class LeaveDetailActivity : BaseActivity(){
    
        private val keyBoardBugWorkaround by lazy {
            AndroidBug5497Workaround(this)
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
        }
    
        override fun onResume() {
            keyBoardBugWorkaround.addListener()
            super.onResume()
        }
    
        override fun onPause() {
            keyBoardBugWorkaround.removeListener()
            super.onPause()
        }
    }
    

提交回复
热议问题