Android 状态栏总结

文章目录

  • Android 状态栏总结
    • 全屏+状态栏无文字图标
    • 全屏+状态栏有文字图标
    • 工具类
      • BarUtils
    • 沉浸式状态栏
    • 代码下载

Android 状态栏总结

全屏+状态栏无文字图标

Android 状态栏总结_第1张图片

方式一:设置主题

<style name="FullScreenTheme" parent="Theme.MyStatusBar">
    
    "windowActionBar">false
    
    "windowNoTitle">true
    
    "android:windowFullscreen">true
    
    "android:statusBarColor">@android:color/transparent
    
    "android:windowTranslucentNavigation">true
style>
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    BarUtils.fitsNotchScreen(this)
    setContentView(R.layout.activity_status_bar1)
}

方式二:代码设置

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    BarUtils.fitsNotchScreen(this)
    BarUtils.fullScreen(this)
    setContentView(R.layout.activity_status_bar1)
}

全屏+状态栏有文字图标

Android 状态栏总结_第2张图片

方式一:设置主题

<style name="FullScreenTheme" parent="Theme.MyStatusBar">
    
    "windowActionBar">false
    
    "windowNoTitle">true
    
    "android:statusBarColor">@android:color/transparent
    
    "android:windowTranslucentNavigation">true
style>

方式二:代码设置

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        BarUtils.fitsNotchScreen(this)
        BarUtils.fullScreen2(this)
        setContentView(R.layout.activity_operate_status_bar2)
}

工具类

BarUtils

/**
 * 状态栏工具类
 */
object BarUtils {

//
    /**
     * 刘海屏适配
     */
    fun fitsNotchScreen(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            try {
                val lp: WindowManager.LayoutParams = activity.window.attributes
                lp.layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                activity.window.attributes = lp
            } catch (e: Exception) {
            }
        }
    }

    /**
     * 隐藏ActionBar
     */
    fun setNoTitle(activity: AppCompatActivity) {
        activity.requestWindowFeature(Window.FEATURE_NO_TITLE) // 隐藏标题栏
        activity.supportActionBar?.hide() // 隐藏ActionBar
    }

    /**
     * 全屏显示,状态栏不显示文字图片
     */
    fun fullScreen(activity: AppCompatActivity) {
        setNoTitle(activity)
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        ) // 全屏显示
        activity.window.statusBarColor = Color.TRANSPARENT; // 设置状态栏颜色为透明
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        ) // 导航栏半透明
    }

    /**
     * 全屏显示,状态栏显示文字图片
     */
    fun fullScreen2(activity: AppCompatActivity) {
        setNoTitle(activity)
        activity.window.statusBarColor = Color.TRANSPARENT; // 设置状态栏颜色为透明
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        ) // 导航栏半透明
    }

    /**
     * 隐藏状态栏
     */
    fun hideStatusBar(activity: Activity) {
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

    /**
     * 显示状态栏
     */
    fun showStatusBar(activity: Activity) {
        activity.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

    /**
     * 隐藏底部导航栏
     */
    fun hideNavigationBar(activity: Activity) {
        activity.window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }

    /**
     * 显示底部导航栏
     */
    fun showNavigationBar(activity: Activity) {
        activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
    }
//

//
    /**
     * 修改状态栏颜色
     */
    fun setStatusBarColor(activity: Activity, @ColorInt color: Int) {
        activity.window.statusBarColor = color
    }

    /**
     * 修改状态栏颜色,通过Color资源
     */
    fun setStatusBarColorRes(activity: Activity, @ColorRes colorResId: Int) {
        setStatusBarColor(activity, ContextCompat.getColor(activity, colorResId))
    }

    /**
     * 亮色状态栏,图片和文字是黑色的
     */
    fun setLightStatusBar(activity: Activity) {
        val flags = activity.window.decorView.systemUiVisibility
        activity.window.decorView.systemUiVisibility = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }

    /**
     * 暗色状态栏,图片和文字是白色的
     */
    fun setDarkStatusBar(activity: Activity) {
        val flags =
            activity.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        activity.window.decorView.systemUiVisibility =
            flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }
//

//
    /**
     * 获取状态栏高度
     */
    fun getStatusBarHeight(context: Context): Int {
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

    /**
     * 获取状态栏高度
     */
    fun getNavigationBarHeight(context: Context): Int {
        val resourceId: Int =
            context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

    /**
     * 是否有导航栏
     */
    fun isNavigationBar(activity: Activity): Boolean {
        val vp = activity.window.decorView as? ViewGroup
        if (vp != null) {
            for (i in 0 until vp.childCount) {
                vp.getChildAt(i).context.packageName
                if (vp.getChildAt(i).id != -1 && "navigationBarBackground" ==
                    activity.resources.getResourceEntryName(vp.getChildAt(i).id)
                ) return true
            }
        }
        return false
    }
//
}

沉浸式状态栏

代码下载

你可能感兴趣的:(Android,android,状态栏)