Android完全透明状态栏?

Muh*_*Ali 142 android statusbar

我搜索了文档,但只找到了这个: 链接.哪个用来使酒吧半透明?我要做的是让状态栏完全透明(如下图所示),并使其向后兼容APK <19: 在此输入图像描述

我的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>
Run Code Online (Sandbox Code Playgroud)

我能做什么:

在此输入图像描述

Dan*_*son 315

您需要做的就是在主题中设置这些属性:

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Run Code Online (Sandbox Code Playgroud)

您希望拥有透明状态栏的活动/容器布局需要设置此属性:

android:fitsSystemWindows="true"
Run Code Online (Sandbox Code Playgroud)

通常不可能在pre-kitkat上执行此操作,看起来你可以这样做但是一些奇怪的代码就是这样.

编辑:我会推荐这个lib:https://github.com/jgilfelt/SystemBarTint,用于许多pre-lollipop状态栏颜色控制.

经过深思熟虑后,我了解到完全禁用半透明的答案或棒棒糖状态栏和导航栏上的任何颜色都是在窗口上设置此标志:

// In Activity's onCreate() for instance
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);
}
Run Code Online (Sandbox Code Playgroud)

没有其他主题是必要的,它产生这样的东西:

在此输入图像描述

  • 我使用了代码,然后添加了一个图像作为背景,然后将"android:fitsSystemWindows ="true"`添加到我的activity_main.xml中.系统条是半透明的而不是透明的. (12认同)
  • 注意:使用 FLAG_LAYOUT_NO_LIMITS 标志也会将您的布局放在底部的虚拟导航栏下。一般来说,你不想这样做......所以和其他人说的一样:-( (10认同)
  • 那么我相信在这种情况下你需要的就是在你的主题中添加`<item name ="android:statusBarColor"> @ android:color/transparent </ item>` (7认同)
  • 是的,我们是,感谢您的耐心等待,因为我在过去的几个小时内一直试图这样做,所以我几乎处于倾斜状态.上面的代码不起作用,但我将`android:windowTranslucentStatus`设置为false,它使状态栏透明.现在我用navigaton bar做了同样的事情并添加了`<item name ="android:navigationBarColor"> @ android:color/transparent </ item>`但它使两个条都变灰.啊! (5认同)
  • 要使"adjustPan"工作,请使用`w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);` (4认同)
  • 嗯,我们越来越近了:D尝试在主题中设置这个`<item name ="android:windowDrawsSystemBarBackgrounds"> true </ item>`我相信它实际上是告诉android使用你设置的statusBarColor属性(https ://developer.android.com/reference/android/R.attr.html#windowDrawsSystemBarBackgrounds) (3认同)
  • @DanielWilson我可以使状态栏和导航栏透明化,或者使状态栏变得半透明。我该如何仅使状态栏透明而不触摸导航栏。我已经尝试了你们在这里讨论的所有选项:) (2认同)
  • 不适用于 Pixel4a Android 11 (2认同)

Cod*_*fee 24

您可以使用以下代码使状态栏透明。 查看带有红色突出显示的图像,可帮助您识别以下代码的使用

1]

您的 Android 应用程序的 Kotlin 代码片段

步骤:1在创建方法中写下代码

if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
    setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
}
if (Build.VERSION.SDK_INT >= 19) {
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
    setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
    window.statusBarColor = Color.TRANSPARENT
}
Run Code Online (Sandbox Code Playgroud)

步骤 2:您需要在下面的代码中描述的 SetWindowFlag 方法。

private fun setWindowFlag(bits: Int, on: Boolean) {
    val win = window
    val winParams = win.attributes
    if (on) {
        winParams.flags = winParams.flags or bits
    } else {
        winParams.flags = winParams.flags and bits.inv()
    }
    win.attributes = winParams
}
Run Code Online (Sandbox Code Playgroud)

您的 Android 应用程序的 Java 代码片段:

Step1:主要活动代码

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);
}
Run Code Online (Sandbox Code Playgroud)

Step2: SetWindowFlag 方法

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);
}
Run Code Online (Sandbox Code Playgroud)


小智 18

只需将这行代码添加到主java文件中:

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
Run Code Online (Sandbox Code Playgroud)

  • 这也会影响导航栏. (25认同)
  • 这会破坏 SOFT_INPUT_ADJUST_PAN 和 SOFT_INPUT_ADJUST_RESIZE,因此无法滚动 (2认同)

Lio*_*ong 15

我刚在这里找到的

因为已经过去 6 年了,默认的 minSDKAPI 是 21 (Lollipop) *CMIIW。这是我完成透明状态栏且不与导航按钮重叠的方法:

fun setStatusBarTransparent(activity: Activity, view: View) {
    activity.apply {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = ContextCompat.getColor(this, R.color.transparent)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        ViewCompat.setOnApplyWindowInsetsListener(view) { root, windowInset ->
            val inset = windowInset.getInsets(WindowInsetsCompat.Type.systemBars())
            root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                leftMargin = inset.left
                bottomMargin = inset.bottom
                rightMargin = inset.right
                topMargin = 0
            }
            WindowInsetsCompat.CONSUMED
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要再次显示状态栏,只需进行一些更改:

fun setStatusBarShown(activity: Activity, view: View) {
    activity.apply {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = ContextCompat.getColor(this, R.color.transparent)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        ViewCompat.setOnApplyWindowInsetsListener(view) { root, windowInset ->
            val inset = windowInset.getInsets(WindowInsetsCompat.Type.systemBars())
            val inset1 = windowInset.getInsets(WindowInsetsCompat.Type.statusBars())
            root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                leftMargin = inset.left
                bottomMargin = inset.bottom
                topMargin = inset1.top
                rightMargin = inset.right
            }
            WindowInsetsCompat.CONSUMED
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在活动中调用该函数时(我也使用视图绑定),我将该函数放入名为 so 的Object类中。UiUtils它看起来像这样:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ...
    UiUtils.setStatusBarTransparent(this, bind.root)
    ...
}
Run Code Online (Sandbox Code Playgroud)

希望我的回答可以帮助到大家:)


Dav*_*son 13

您可以使用外部库StatusBarUtil:

添加到您的模块级别build.gradle:

compile 'com.jaeger.statusbarutil:library:1.4.0'
Run Code Online (Sandbox Code Playgroud)

然后,您可以将以下util用于Activity以使状态栏透明:

StatusBarUtil.setTransparent(Activity activity)
Run Code Online (Sandbox Code Playgroud)

例:

Lollipop和KitKat上的透明状态栏

  • 一个lib只是为了解决这个问题,就像用火焰喷射器点燃蜡烛一样 (12认同)

And*_*mon 11

对于具有日/夜支持的 API > 23,您可以使用下面的扩展。需要理解的重要部分是android:fitsSystemWindows="true"使用填充在插图中移动(就像使用工具栏一样)。因此,将其放置在根布局中是没有意义的(除了 DrawerLayout、CoordinatorLayout,...这些使用它们自己的实现)。

在此输入图像描述

<style name="Theme.YourApp.DayNight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="android:windowLightStatusBar">@bool/isDayMode</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
<androidx.constraintlayout.widget.ConstraintLayout
    ...>

    <com.google.android.material.appbar.MaterialToolbar
        ...
        android:fitsSystemWindows="true">

</androidx.constraintlayout.widget.ConstraintLayout
Run Code Online (Sandbox Code Playgroud)
fun Activity.transparentStatusBar() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    } else {
        window.setDecorFitsSystemWindows(false)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样调用它:

class YourActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        transparentStatusBar()
    }
}
Run Code Online (Sandbox Code Playgroud)

查看克里斯·贝恩斯 (Chris Banes) 的这些幻灯片:成为窗户安装大师

编辑:如果您对导航栏后面浮动的内容有问题,请使用

// using Insetter
binding.yourViewHere.applySystemWindowInsetsToPadding(bottom = true)
Run Code Online (Sandbox Code Playgroud)


Par*_*rSa 8

适用于Android KitKat或更高版本(对于那些想要透明化状态栏并且不操纵NavigationBar的用户,因为所有这些答案也将透明化NavigationBar!)

最简单的方法:

styles.xml (v19)如果不知道如何将这三行代码放在->(v19)中,只需将它们写成默认值styles.xml,然后使用alt+ enter自动创建即可:

<item name="android:windowFullscreen">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:fitsSystemWindows">false</item>
Run Code Online (Sandbox Code Playgroud)

现在,转到您的MainActivityClass并将此Method放在类的onCreate中:

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);
}
Run Code Online (Sandbox Code Playgroud)

然后将此代码放入onCreateActivity 的方法中:

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);
    }
    //make fully Android Transparent Status bar
    if (Build.VERSION.SDK_INT >= 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
Run Code Online (Sandbox Code Playgroud)

而已!

  • 这正确回答了OP的问题。这应该是公认的答案。 (2认同)

Ham*_*tah 8

这是我经过大量搜索后发现的简单方法。

步骤1

在你的主题中放置这个项目

        <item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
Run Code Online (Sandbox Code Playgroud)

第2步

在主要活动中

WindowCompat.setDecorFitsSystemWindows(window, false)
Run Code Online (Sandbox Code Playgroud)

如果您使用 BottomNavigationBar,则非常重要

在某些设备中,如果在应用程序中使用 API 30+,您会发现系统导航栏与底部导航栏重叠。

这正在解决这个问题。

  if (Build.VERSION.SDK_INT >= 30) {
        // Root ViewGroup of my activity
        val root = findViewById<ConstraintLayout>(R.id.root)

        ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets ->

            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

            // Apply the insets as a margin to the view. Here the system is setting
            // only the bottom, left, and right dimensions, but apply whichever insets are
            // appropriate to your layout. You can also update the view padding
            // if that's more appropriate.

            view.layoutParams =  (view.layoutParams as FrameLayout.LayoutParams).apply {
                leftMargin = insets.left
                bottomMargin = insets.bottom
                rightMargin = insets.right
            }

            // Return CONSUMED if you don't want want the window insets to keep being
            // passed down to descendant views.
            WindowInsetsCompat.CONSUMED
        }

    }
Run Code Online (Sandbox Code Playgroud)


Ann*_*eed 7

共有三个步骤:

\n

1)只需在 OnCreate 方法中使用此代码段

\n
  // FullScreen\n  getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, \n  WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);\n
Run Code Online (Sandbox Code Playgroud)\n

如果您\xe2\x80\x99正在处理Fragment,则应该将此代码段放入您的activity\xe2\x80\x99s OnCreate方法中。

\n

2)请务必在 /res/values-v21/styles.xml 中设置透明度:

\n
<item name="android:statusBarColor">@android:color/transparent</item>\n
Run Code Online (Sandbox Code Playgroud)\n

或者您可以以编程方式设置透明度:

\n
getWindow().setStatusBarColor(Color.TRANSPARENT);\n
Run Code Online (Sandbox Code Playgroud)\n

3)无论如何你应该在styles.xml中添加代码段

\n
<item name="android:windowTranslucentStatus">true</item>\n
Run Code Online (Sandbox Code Playgroud)\n

注意:此方法仅适用于 API 21 及更高版本。

\n


Sum*_*hoo 7

我发现摆弄 styles.xml 和 Activity 太麻烦,因此创建了一个通用实用方法,其中设置了以下选项

爪哇

Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
window.setStatusBarColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)

科特林DSL

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
}
Run Code Online (Sandbox Code Playgroud)

这就是实现透明状态栏所需的全部工作。希望这可以帮助。


Shr*_*hek 7

简单明了,适用于几乎所有用例(API 级别 16 及以上):

  1. 在您的应用主题中使用以下标签使状态栏透明:

    <item name="android:statusBarColor">@android:color/transparent</item>

  2. 然后在您的活动的 onCreate 方法中使用此代码。

    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
    Run Code Online (Sandbox Code Playgroud)

这就是你需要做的全部;)

您可以从开发人员文档中了解更多信息。我还建议您阅读这篇博文。

科特林代码

    val decorView = window.decorView
    decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
Run Code Online (Sandbox Code Playgroud)

在这里查看我的另一个答案


小智 6

使用android:fitsSystemWindows="false"您的顶面布置图

  • 这也会影响导航栏 (5认同)

Tho*_*sse 6

在状态栏下绘制布局:

values / styles.xml

<item name="android:windowTranslucentStatus">true</item>
Run Code Online (Sandbox Code Playgroud)

values-v21 / styles.xml

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
Run Code Online (Sandbox Code Playgroud)

使用已经处理了fitsSystemWindows参数的CoordinatorLayout / DrawerLayout或创建自己的布局来像这样:

public class FitsSystemWindowConstraintLayout extends ConstraintLayout {

    private Drawable mStatusBarBackground;
    private boolean mDrawStatusBarBackground;

    private WindowInsetsCompat mLastInsets;

    private Map<View, int[]> childsMargins = new HashMap<>();

    public FitsSystemWindowConstraintLayout(Context context) {
        this(context, null);
    }

    public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (ViewCompat.getFitsSystemWindows(this)) {
            ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
                    FitsSystemWindowConstraintLayout layout = (FitsSystemWindowConstraintLayout) view;
                    layout.setChildInsets(insets, insets.getSystemWindowInsetTop() > 0);
                    return insets.consumeSystemWindowInsets();
                }
            });
            setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            TypedArray typedArray = context.obtainStyledAttributes(new int[]{android.R.attr.colorPrimaryDark});
            try {
                mStatusBarBackground = typedArray.getDrawable(0);
            } finally {
                typedArray.recycle();
            }
        } else {
            mStatusBarBackground = null;
        }
    }

    public void setChildInsets(WindowInsetsCompat insets, boolean draw) {
        mLastInsets = insets;
        mDrawStatusBarBackground = draw;
        setWillNotDraw(!draw && getBackground() == null);

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                if (ViewCompat.getFitsSystemWindows(this)) {
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) child.getLayoutParams();

                    if (ViewCompat.getFitsSystemWindows(child)) {
                        ViewCompat.dispatchApplyWindowInsets(child, insets);
                    } else {
                        int[] childMargins = childsMargins.get(child);
                        if (childMargins == null) {
                            childMargins = new int[]{layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin};
                            childsMargins.put(child, childMargins);
                        }
                        if (layoutParams.leftToLeft == LayoutParams.PARENT_ID) {
                            layoutParams.leftMargin = childMargins[0] + insets.getSystemWindowInsetLeft();
                        }
                        if (layoutParams.topToTop == LayoutParams.PARENT_ID) {
                            layoutParams.topMargin = childMargins[1] + insets.getSystemWindowInsetTop();
                        }
                        if (layoutParams.rightToRight == LayoutParams.PARENT_ID) {
                            layoutParams.rightMargin = childMargins[2] + insets.getSystemWindowInsetRight();
                        }
                        if (layoutParams.bottomToBottom == LayoutParams.PARENT_ID) {
                            layoutParams.bottomMargin = childMargins[3] + insets.getSystemWindowInsetBottom();
                        }
                    }
                }
            }
        }

        requestLayout();
    }

    public void setStatusBarBackground(Drawable bg) {
        mStatusBarBackground = bg;
        invalidate();
    }

    public Drawable getStatusBarBackgroundDrawable() {
        return mStatusBarBackground;
    }

    public void setStatusBarBackground(int resId) {
        mStatusBarBackground = resId != 0 ? ContextCompat.getDrawable(getContext(), resId) : null;
        invalidate();
    }

    public void setStatusBarBackgroundColor(@ColorInt int color) {
        mStatusBarBackground = new ColorDrawable(color);
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDrawStatusBarBackground && mStatusBarBackground != null) {
            int inset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
            if (inset > 0) {
                mStatusBarBackground.setBounds(0, 0, getWidth(), inset);
                mStatusBarBackground.draw(canvas);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

main_activity.xml

<FitsSystemWindowConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        android:src="@drawable/toolbar_background"
        app:layout_constraintBottom_toBottomOf="@id/toolbar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/transparent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Content"
            android:textSize="48sp" />
    </LinearLayout>
</FitsSystemWindowConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

结果:

屏幕截图:
屏幕截图

  • 那不是透明状态栏,那是半透明状态栏 (4认同)

Aha*_*kat 6

完全透明的StatusBar和NavigationBar

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

    transparentStatusAndNavigation();
}


private void transparentStatusAndNavigation() {
    //make full transparent statusBar
    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, true);
    }
    if (Build.VERSION.SDK_INT >= 19) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        );
    }
    if (Build.VERSION.SDK_INT >= 21) {
        setWindowFlag(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, false);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
    }
}

private void setWindowFlag(final int bits, boolean on) {
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}
Run Code Online (Sandbox Code Playgroud)


Fel*_*hos 5

这是kotlin的扩展,可以实现此功能:

fun Activity.setTransparentStatusBar() {
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.statusBarColor = Color.TRANSPARENT
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)