Android 中的半透明状态栏

Translucent status bar in Android

我正在使用以下布局 (main_activity.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:fitsSystemWindows="true"
        android:text="@string/large_text" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

具有以下样式-v21

<resources>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>

但是我还是无法存档透明状态栏

看起来像这样

但我想要这样的东西(新 Google 报亭应用程序)

注意:我正在使用

compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:design:25.1.1'
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT); } }

}

在您的 activity 中使用它。但是改变状态栏颜色只允许在android L

之后

请从您的 CoordinatorLayout 中删除这一行:

android:fitsSystemWindows="true"

已编辑, 结果:

试试这个:

将此添加到您的应用主题中:

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

android:fitsSystemWindows=”true”设置为根容器

现在在您的 activity onCreate() 中:

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);
    }

已更新,结果:

您可以使 snackbar 在屏幕顶部绘制:

Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();

有关详细信息,请参阅 here

下面的代码使 API21+ 的状态栏透明但导航栏不透明。

注意:您不应该设置 android:fitsSystemWindows="true"

override fun onCreate(savedInstanceState: Bundle?) {
      setupContentWindow()
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
}

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

在你希望其布局状态栏透明的activity的onCreate中添加这两行

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}

无需更改您的应用主题或 xml 布局。如果您不想这样做,您的栏不必在整个应用程序中都是透明的。

好像有些人的状态栏不是透明的而是灰色的。为避免此问题,请在 activity:

中执行下一步
override fun onCreate(savedInstanceState: Bundle?) {
    ...
    window.apply {
        clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        statusBarColor = Color.TRANSPARENT
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
}