current position:Home>Various modes of Android status bar operation

Various modes of Android status bar operation

2022-01-27 02:22:05 Tommy porridge

    fun getStatusBarHeight(context: Context): Int {
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            return context.resources.getDimensionPixelSize(resourceId)
        }
        return 0
    }

    fun setStatusBarTransparent(activity: Activity) {
        if (isFromM()) {
//            val decorView = activity.window.decorView
//            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//            decorView.systemUiVisibility = option
            activity.window.statusBarColor = Color.TRANSPARENT
        }
    }

    fun setStatusBarBgColor(activity: Activity, @ColorInt color: Int) {
        if (isFromM()) {
//            val decorView = activity.window.decorView
//            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//            decorView.systemUiVisibility = option
            activity.window.statusBarColor = color
        }
    }

    fun setStatusBarIconColor(activity: Activity, blackIcon: Boolean) {
        var option = 0;
        if (blackIcon) {
            option = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR  // Black characters on white background 
        } else {
            option = View.SYSTEM_UI_FLAG_VISIBLE;     // Default , Black bottom white character 
        }
        activity.window.decorView.systemUiVisibility = option
    }

    fun setStatusBarFullScreen(activity: Activity, isFloat: Boolean) {
        val decorView = activity.window.decorView
        val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        decorView.systemUiVisibility = option
    }
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/white</item>
        <item name="android:windowLightStatusBar">true</item>  <!--  above <item name="android:windowLightStatusBar">true</item> Is to use the bright status bar  -->
    </style>

 setSystemUiVisibility(int visibility) The types of arguments passed in are as follows :

1.View.SYSTEM_UI_FLAG_VISIBLE : Status bar and Activity coexistence ,Activity Not full screen . That is to apply the normal display screen

2.View.SYSTEM_UI_FLAG_FULLSCREEN :Activity Full screen display , And the status bar is overwritten

3. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN :Activity Full screen display , But the status bar will not be overwritten , It's normal display , It's just Activity Top cloth   The Bureau will be covered

4.View.INVISIBLE : Activity Full screen display , hide the status bar

copyright notice
author[Tommy porridge],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270222035466.html

Random recommended