[英]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:实际上,您需要做四件事:
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.