StatusBarUtil icon indicating copy to clipboard operation
StatusBarUtil copied to clipboard

希望支持全屏的DialogFragment设置状态栏

Open imliujun opened this issue 7 years ago • 0 comments

建议将所有参数为Activity的方法增加一个Window参数版本,这样我们使用全屏的DialogFragment可以针对DialogFragment的Window去设置沉浸式。 例如:

 /**
     * 设置状态栏颜色
     *
     * @param window         需要设置的activity
     * @param color          状态栏颜色值
     * @param statusBarAlpha 状态栏透明度
     */
    fun setColor(fragment: Fragment, window: Window, color: Int, statusBarAlpha: Int) {
        if (fragment.activity == null) return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.statusBarColor = calculateStatusColor(color, statusBarAlpha)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            // 生成一个状态栏大小的矩形
            val statusView = createStatusBarView(fragment.activity!!, color, statusBarAlpha)
            // 添加 statusView 到布局中
            val decorView = window.decorView as ViewGroup
            decorView.addView(statusView)
            setRootView(fragment)
        }
    }

/**
     * 为头部是 ImageView 的界面设置状态栏透明
     *
     * @param activity       需要设置的activity
     * @param statusBarAlpha 状态栏透明度
     * @param needOffsetView 需要向下偏移的 View
     */
    fun setTranslucentForImageView(
            window: Window,
            @IntRange(from = 0, to = 255) statusBarAlpha: Int,
            needOffsetView: View?
    ) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return
        }
        setTransparentForWindow(window)
        addTranslucentView(window.context, window, statusBarAlpha)
        if (needOffsetView != null) {
            val haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET)
            if (haveSetOffset != null && haveSetOffset == true) {
                return
            }
            val layoutParams = needOffsetView.layoutParams as ViewGroup.MarginLayoutParams
            layoutParams.setMargins(layoutParams.leftMargin,
                    layoutParams.topMargin + getStatusBarHeight(window.context),
                    layoutParams.rightMargin, layoutParams.bottomMargin)
            needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true)
        }
    }

 /**
     * 添加半透明矩形条
     *
     * @param activity       需要设置的 activity
     * @param statusBarAlpha 透明值
     */
    private fun addTranslucentView(context: Context, window: Window, @IntRange(from = 0,
            to = 255) statusBarAlpha: Int) {
        val contentView = window.findViewById(android.R.id.content) as ViewGroup
        val fakeTranslucentView = contentView.findViewById<View>(FAKE_TRANSLUCENT_VIEW_ID)
        if (fakeTranslucentView != null) {
            if (fakeTranslucentView.visibility == View.GONE) {
                fakeTranslucentView.visibility = View.VISIBLE
            }
            fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0))
        } else {
            contentView.addView(createTranslucentStatusBarView(context, statusBarAlpha))
        }
    }
    
/**
     * 设置根布局参数
     */
    private fun setRootView(fragment: Fragment) {
        val view = fragment.view
        if (view != null) {
            view.fitsSystemWindows = true
            if (view is ViewGroup) {
                view.clipToPadding = true
            }
        }
    }
    
    /**
     * 设置透明
     */
    private fun setTransparentForWindow(window: Window) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = Color.TRANSPARENT
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

imliujun avatar May 22 '18 04:05 imliujun