如何制作透明状态栏

时间:2018-05-25 21:26:58

标签: android android-layout

我正在尝试设置透明状态栏(例如预览),但它看起来像视频https://yadi.sk/i/HcYJa3VB3WYGvL

预览

enter image description here

我试过设置样式xml的

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

在不同的布局中,也设置

fitsSystemWindows = "true"

MainActivity.xml:

<android.support.v4.widget.DrawerLayout
    ...
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_container"
        android:fitsSystemWindows="true"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:background="@color/color_primary">

            <android.support.design.widget.CollapsingToolbarLayout
                ...
                app:contentScrim="@color/color_primary"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/profile_image_size"
                    android:scaleType="centerCrop"
                    android:src="@drawable/user_photo"
                    app:layout_collapseMode="parallax"
                    android:fitsSystemWindows="true"/>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:theme="@style/AppTheme.PopupOverlay"
                    app:titleTextColor="@color/white"
                    app:layout_collapseMode="pin"/>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

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

            ...

        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.FloatingActionButton
            .../>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        ...>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

V21 / style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_primary_dark</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/color_accent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>   

3 个答案:

答案 0 :(得分:1)

有两种不同的情况:

1.如果您想要稍微透明的状态栏(如您在问题中所述)

<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>

2.对于完全透明的状态栏:

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

检查这个answer,了解事情是如何运作的。

答案 1 :(得分:1)

在您的主题中添加此内容

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

将android:fitsSystemWindows =“true”设置为root容器

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

添加您的活动

答案 2 :(得分:0)

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

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

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

android:fitsSystemWindows="true"

已经回答了这个问题。关注:Android Completely transparent Status Bar?