Android完全透明状态栏?

267

我已经搜索了文档,但只找到这个链接:Link。它用于使栏 半透明?我想要做的是让状态栏完全透明(如下图所示),并且向后兼容APK<19:

enter image description here

我的styles.xml:

<resources xmlns:tools="http://schemas.android.com/tools">

  <style name="AppTheme" parent="Theme.AppCompat.Light">
  <item name="android:actionBarStyle">@style/ThemeActionBar</item>
  <item name="android:windowActionBarOverlay">true</item>
  <!-- Support library compatibility -->
  <item name="actionBarStyle">@style/ThemeActionBar</item>
  <item name="windowActionBarOverlay">true</item>
  </style>

  <style name="ThemeActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid">
  <item name="android:background"> @null </item>
  <!-- Support library compatibility -->
  <item name="background">@null</item>
  <item name="android:displayOptions"> showHome | useLogo</item>
  <item name="displayOptions">showHome|useLogo</item>

  </style>

</resources>

我能做的事情:

输入图像描述


来自官方消息:https://medium.com/androiddevelopers/translucent-systembars-the-right-way-across-api-levels-and-themes-6d7ddda21396 - Zero
39个回答

1
为了实现完全透明的状态栏: 首先在themes.xml中创建自定义主题:
<style name="Theme.QuiloThemeLight" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentNavigation">true</item>
</style>

Java:

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    getWindow().setStatusBarColor(Color.TRANSPARENT);

Kotlin :

activity.window.apply {
        clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        statusBarColor = Color.TRANSPARENT
    }

1

针对来自谷歌的最新更新,这是我的代码。它将使状态栏和导航栏透明:

将您的主题更改为以下内容:

 <style name="Theme.Pink" 
 parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/pink_primary</item>
    <item name="colorPrimaryVariant">@color/pink_dark</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/pink_light</item>
    <item name="colorSecondaryVariant">@color/pink_primary</item>
    <item name="colorOnSecondary">@color/black</item>
    <item name="android:textColor">?attr/colorOnPrimary</item>
    <!-- Status bar color. -->
    <!-- Customize your theme here. -->
 //transparent statusbar
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowIsTranslucent">true</item>
//transparent navigationbar
    <item name="android:windowTranslucentNavigation">true</item> 
 </style>

然后在布局文件(xml文件)中:添加2个属性point_top和point_bottom来确定应用程序的其他组件的边距值的位置:

    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_activity">

    <TextView
        android:id="@+id/point_top"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    

    <TextView
        android:id="@+id/point_bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
   
       <.....>

   </androidx.constraintlayout.widget.ConstraintLayout>

然后在baseActivity或yourActivity中:在onCreate中添加以下代码:

 override fun onCreate(savedInstanceState: Bundle?) {
   
    super.onCreate(savedInstanceState)
    binding = setLayout()
    //set inset for view
    ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, insets ->
        val paramsTop =
            findViewById<TextView>(R.id.point_top).layoutParams as MarginLayoutParams
        paramsTop.setMargins(0, insets.getInsets(WindowInsetsCompat.Type.systemBars()).top, 0, 0)
        findViewById<TextView>(R.id.point_top).layoutParams = paramsTop
        val paramsBottom =
            findViewById<TextView>(R.id.point_bottom).layoutParams as MarginLayoutParams
        paramsBottom.setMargins(0, 0, 0, insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom)
        findViewById<TextView>(R.id.point_bottom).layoutParams = paramsBottom
        insets.consumeSystemWindowInsets()

    }

    setContentView(binding.root)
}

希望这能帮助大家。


1
虽然以上所有答案都围绕着同一基本思路,你可以使用上面的其中一个示例来使其在简单布局中运作。但是我想在使用滑动“全屏幕”(标签栏除外)片段导航时更改背景颜色,并保持常规导航、选项卡和操作栏。
仔细阅读Anton Hadutski的文章后,我对发生的事情有了更好的理解。
我有一个带有ConstraintLayout(即容器)的DrawerLayout,其中包括主要片段和BottomNavigationView的工具栏。
DrawerLayoutfitsSystemWindows设置为true是不够的,您需要同时设置DrawerLayoutConstraintLayout。假设状态栏透明,则状态栏颜色现在与ConstraintLayout的背景颜色相同。
但是,所包含的片段仍具有状态栏插入,因此在其上方动画另一个“全屏幕”片段并不会更改状态栏的颜色。
在参考文章中,将一小段代码放入ActivityonCreate方法中:
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container)) { view, insets ->
        insets.replaceSystemWindowInsets(
                insets.systemWindowInsetLeft,
                0,
                insets.systemWindowInsetRight,
                insets.systemWindowInsetBottom
        )
    }

除了现在的工具栏没有考虑状态栏的高度之外,一切都很好。参考文章后,我们得到了一个完全可行的解决方案:
val toolbar = findViewById<Toolbar>(R.id.my_toolbar)
    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container)) { view, insets ->
        val params = toolbar.layoutParams as ViewGroup.MarginLayoutParams
        params.topMargin = insets.systemWindowInsetTop
        toolbar.layoutParams = params
        insets.replaceSystemWindowInsets(
                insets.systemWindowInsetLeft,
                0,
                insets.systemWindowInsetRight,
                insets.systemWindowInsetBottom
        )
    }

主活动布局文件 main_activity.xml(请注意 Toolbar 中的 marginTop 是为了预览目的,它将被代码替换):
<?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        >

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/green"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintTop_toTopOf="@id/container"
            android:layout_marginTop="26dp" 
            android:background="@android:color/transparent"
            ...>
            ...
        </androidx.appcompat.widget.Toolbar>

        <include layout="@layout/content_main" />
        ...
    </androidx.constraintlayout.widget.ConstraintLayout>
    ...
</androidx.drawerlayout.widget.DrawerLayout>

1
所有需要的是进入MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window g = getWindow();
        g.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        setContentView(R.layout.activity_main);

    }

1
在我的情况下,我完全不会调用 "onCreate"(这是一个React Native应用程序,也可以通过使用React-Native StatusBar组件来解决)。也可以使用以下方法:
override fun onStart() {
        super.onStart()
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
}

1
这里是 Kotlin 扩展:
fun Activity.transparentStatusBar() {

    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.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        window.statusBarColor = Color.TRANSPARENT

    } else
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

}

1

只需调用此函数,它可以在新/旧的安卓 API 上正常工作:

private fun changeScreenSystemUiController(isFullScreen: Boolean) {
    window?.also {
        WindowCompat.setDecorFitsSystemWindows(it, !isFullScreen)
        WindowCompat.getInsetsController(it, it.decorView).apply {
            systemBarsBehavior =
                if (isFullScreen)
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                else
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
            if (isFullScreen)
                hide(WindowInsetsCompat.Type.systemBars())
            else
                show(WindowInsetsCompat.Type.systemBars())
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            it.attributes.layoutInDisplayCutoutMode =
                if (isFullScreen)
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                else
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
        }
    }
}

0
以下代码将创建一个完全透明的状态栏:
package com.demo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
        }
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public static void setWindowFlag(Activity activity, final int bits, boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
}

0
 <item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
            <!--<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>-->

不要使用 windowLightStatusBar,而是使用 statusBarColor = @android:color/transparent

0
将以下代码添加到你的Activity的setContentView()方法之前。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

将这两行代码添加到你的 AppTheme 中。
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

最后一件事,你的minSdkVersion必须是19

minSdkVersion 19

如果用户只想为某些特定活动创建,则这是一个不好的解决方案。 - famfamfam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接