透明状态栏导致windowsoftinputmode失效的问题解决

/**
 * 全透状态栏
 */
protected fun setStatusBarFullTransparent() {
    if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
        val window = window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
    } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        //虚拟键盘也透明
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
}

设置后,mainfest里的windowsoftinputMode设置的值无效,键盘还是会遮挡住布局。

解决方法:在window的content布局加一个布局改变监听。

mChildOfContent.viewTreeObserver.addOnGlobalLayoutListener { this.possiblyResizeChildOfContent() }

完整代码:

class WindowSoftModeAdjustResizeExecutor private constructor(val activity: Activity, private val hasFakeStatusBar: Boolean) {

    private val mChildOfContent: View
    private var usableHeightPrevious: Int = 0
    private val frameLayoutParams: FrameLayout.LayoutParams

    init {
        val content = activity.findViewById<View>(android.R.id.content) as FrameLayout
        mChildOfContent = content.getChildAt(0)
        mChildOfContent.viewTreeObserver.addOnGlobalLayoutListener { this.possiblyResizeChildOfContent() }
        frameLayoutParams = mChildOfContent.layoutParams as FrameLayout.LayoutParams
    }

    private fun possiblyResizeChildOfContent() {
        val usableHeightNow = computeUsableHeight()
        if (usableHeightNow != usableHeightPrevious) {
            val usableHeightSansKeyboard = mChildOfContent.rootView.height
            val heightDifference = usableHeightSansKeyboard - usableHeightNow

            frameLayoutParams.height = usableHeightSansKeyboard - heightDifference

            mChildOfContent.requestLayout()
            usableHeightPrevious = usableHeightNow
        }
    }

    private fun computeUsableHeight(): Int {
        val r = Rect()
        mChildOfContent.getWindowVisibleDisplayFrame(r)
        return if (hasFakeStatusBar) r.bottom - r.top else r.bottom
    }

    companion object {
        // For more information, see https://code.google.com/p/android/issues/detail?id=5497
        // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

        // CREDIT TO Joseph Johnson (http://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.com

        fun assistActivity(activity: Activity, hasFakeStatusBar: Boolean) {
            WindowSoftModeAdjustResizeExecutor(activity, hasFakeStatusBar)
        }
    }
}

在setContentView之后调用

WindowSoftModeAdjustResizeExecutor.assistActivity(this,false)

第二个参数是我app里的沉浸状态栏是假沉浸状态栏,在decorView里加了个和状态栏高度一样的view。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值