简体   繁体   English

如何制作完全透明的状态栏?

[英]How to make completely transparent status bar?

I want to make so my status bar is transparent, but also without icons on it.我想让我的状态栏是透明的,但也没有图标。 I managed to make it so that bar disappeared, but then it left a line that isn't filled with the background.我设法让它消失了,但是它留下了一条没有填充背景的线。 I want to change that so i can actually see the background without any icons being in the way<\/strong> .我想改变它,这样我就可以在没有任何图标<\/strong>的情况下真正看到背景。

Also, I'm testing it on Xiaomi Redmi Note 8T<\/em>另外,我正在小米红米 Note 8T 上进行测试<\/em>

Code (with the result seen on the 1st picture)<\/em>代码(结果见第一张图片)<\/em>

MainActivity.kt MainActivity.kt

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate

class MainActivity : AppCompatActivity() {

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) hideSystemUI()
    }

    private fun hideSystemUI() {SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    window.decorView.systemUiVisibility = 
                (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Set these properties in your theme.在您的主题中设置这些属性。 Thats all就这样

<item name="android:windowTranslucentStatus">true</item>

you can use below lines to make it transparent or can set fix color getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);您可以使用以下几行使其透明或可以设置修复颜色 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); getWindow().setStatusBarColor(ContextCompat.getColor(ContactUsActivity.this,R.color.white));// set status background white getWindow().setStatusBarColor(ContextCompat.getColor(ContactUsActivity.this,R.color.white));//设置状态背景白色

The problem:<\/strong>问题:<\/strong>

Phone's can now have a "cut-out" (for notched phones etc. etc.), and when you hide the status bar properly, the phone thinks "ok, I need to make the former status bar just a black cut-out so it doesn't look silly."手机现在可以有一个“切口”(用于缺口手机等),当您正确隐藏状态栏时,手机会认为“好的,我需要将以前的状态栏设置为黑色切口所以看起来并不傻。”

The solution:<\/strong>解决方案:<\/strong>

There's actually four things you need to do:实际上,您需要做四件事:

  1. Tell Android you don't want status bars in the app (hide them).告诉 Android 您不想在应用程序中使用状态栏(隐藏它们)。 There is now a correct, backwards compatible way of doing this which I will detail below.现在有一种正确的、向后兼容的方法可以做到这一点,我将在下面详细介绍。<\/li>
  2. Tell Android to draw the app behind the cut-out area.告诉 Android 在剪切区域后面绘制应用程序。<\/li>
  3. Tell Android that your layout should NOT fit the system window (if you don't do this, it will STILL put your layout within the space where the status bar used to be)告诉 Android 你的布局不应该适合系统窗口(如果你不这样做,它仍然会将你的布局放在状态栏曾经所在的空间内)<\/li>
  4. Manually adjust the bottom insets, as when you tell Android your layout should not fit the system window (step 3), unfortunately that's going to include the Navigation Bar, so if you don't adjust for this then your layout will be under the nav bar which may be undesirable.手动调整底部插图,当您告诉 Android 您的布局不适合系统窗口时(第 3 步),不幸的是,这将包括导航栏,因此如果您不为此进行调整,那么您的布局将位于导航下方可能不受欢迎的栏。<\/li><\/ol>

    The Code:<\/strong>代码:<\/strong>

    The below is a neat little extension function that will cover steps 1 and 3... (hide the status bars, set decorFitsSystemWindows<\/code> to false).下面是一个简洁的小扩展函数,它将涵盖第 1 步和第 3 步...(隐藏状态栏,将decorFitsSystemWindows<\/code>设置为 false)。 This allows, in your activity to simply put window.setInImmersiveMode()<\/code>这允许在您的活动中简单地放置window.setInImmersiveMode()<\/code>

     fun Window.setInImmersiveMode() { val windowInsetsController = ViewCompat.getWindowInsetsController(decorView) ?: return windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE windowInsetsController.hide(WindowInsetsCompat.Type.statusBars()) WindowCompat.setDecorFitsSystemWindows(this, false) }<\/code><\/pre>

    You need to change your themes.xml to tell Android to draw behind these cutouts, with this line (api 27 and above only, hence the tools:targetApi part):您需要更改您的主题.xml 以告诉 Android 在这些切口后面绘制,使用此行(仅限 api 27 及更高版本,因此工具:targetApi 部分):

     <item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges<\/item><\/code><\/pre>

    And finally, there's a slightly more complex bit of code to handle setting up the bottom insets for your layout - again I've made it into an extension function so you can call view.setupInsets()<\/code> :最后,还有一些稍微复杂的代码来处理为您的布局设置底部插图 - 我再次将它变成了一个扩展函数,因此您可以调用view.setupInsets()<\/code> :

     fun View.setupInsets() { ViewCompat.setOnApplyWindowInsetsListener(this) { view, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply { bottomMargin = insets.bottom } WindowInsetsCompat.CONSUMED } }<\/code><\/pre>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM