从使用BottomSheetDialogFragment到放弃,改直接使用Popupwindow

我项目中有个需求,是要从下面滑出一个页面,点x往下滑走。这个页面占全屏(显示状态栏)当然,我首先想到的是用BottomSheetDialogFragment,因为我知道这就是用来做底出滑出效果的。可是用起来发现这个BottomSheetDialog用起来有这么多不爽的地方:

1. 你写的布局不是是全屏的,但他默认只出来一部分。于是用这样的代码解决了

val layoutParams = bottomSheet!!.layoutParams
val windowHeight = ScreenUtil.getScreenHeight(requireContext())
if (layoutParams != null) {
    layoutParams.height = windowHeight
}
bottomSheet.layoutParams = layoutParams

还有这样的代码

dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

但是发现滑动出来了会导致状态栏百年城黑底白色的(图标,和字都变成白色的),我的要求是不让他变成白色的。

于是我又加了这些代码

true
@android:color/transparent
false

@style/store_MyBottomSheetAnimation

发现没有黑底了,是白底的,但状态栏上的图标和字还是变成白色的,导致什么都看不见。

然后继续搜索,有人说是BottomSheetDialog高度导致状态栏变色,要自定义BottomSheetDialog,于是加了这些代码:

package com.maka.store.ui.home

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.ViewGroup
import com.common.base.utils.ScreenUtil
import com.google.android.material.bottomsheet.BottomSheetDialog


class MyBottomSheetDialog(context: Context,style:Int): BottomSheetDialog(context,style) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val screenHeight = ScreenUtil.getScreenHeight(ownerActivity)
        val statusBarHeight = getStatusBarHeight(context)
        val dialogHeight = screenHeight- statusBarHeight
        window!!.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, if (dialogHeight == 0) ViewGroup.LayoutParams.MATCH_PARENT else dialogHeight)
    }

    private fun getScreenHeight(activity: Activity?): Int {
        val displaymetrics = DisplayMetrics()
        activity!!.windowManager.defaultDisplay.getMetrics(displaymetrics)
        return displaymetrics.heightPixels
    }

    private fun getStatusBarHeight(context: Context): Int {
        var statusBarHeight = 0
        val res: Resources = context.getResources()
        val resourceId: Int = res.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }
}

加了这些代码,状态栏是没问题了,但页面高度又有问题了,在有虚拟键盘和无虚拟键盘时的高度有问题。

最终选择不使用BottomSheetDialogFragment,而是用Popupwindow实现滑动效果:

var popupWindow: PopupWindow? = null

private fun initPopupWindow() {
    var contentView = LayoutInflater.from(this).inflate(R.layout.popup_home_add, null, false)

    var ivClose = contentView.findViewById(R.id.iv_close) as ImageView
    ivClose.setOnClickListener {
        popupWindow?.dismiss()
    }
    //实例化PopupWindow并设置宽高
    popupWindow = PopupWindow(
        contentView,
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
    )
    popupWindow?.setBackgroundDrawable(BitmapDrawable())
    popupWindow?.setOutsideTouchable(true)  //点击外部消失,这里因为PopupWindow填充了整个窗口,所以这句代码就没用了
    popupWindow?.setTouchable(true) //设置可以点击
    popupWindow?.setAnimationStyle(R.style.store_AnimBottom)//进入退出的动画
}

private fun showPopWindow() {
    popupWindow?.showAtLocation(root, Gravity.BOTTOM, 0, 0)
}

切入动画:




    



切出动画:




    


你可能感兴趣的:(笔记,动画)