【问题标题】:Cannot make status bar translucent无法使状态栏半透明
【发布时间】:2015-09-15 11:09:26
【问题描述】:

编辑: 更相关的问题发布在这里:MaterialDrawer library turns a translucent status bar into a opaque one


我知道它可以在 API19+ 中完成。我也读过thisthis 似乎很容易做到。但是我无法完成它。这是我在values-v19 文件夹中的主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

我还将 appcompat 主题更改为Theme.Holo.Light.NoActionBar.TranslucentDecor(当然使用Activity 而不是AppCompatActivity);没有任何改变。

这是来自 genymotion emulator api-22 的屏幕截图(实心状态栏;在真实设备上的结果相同):

现在奇怪的是:在布局的预览窗口中,当您选择 api-23(在 appcompat 和 holo 主题上)时,我得到了这个结果(半透明):

api-19(也是 api-21 和 22)的预览是这样的:

这很令人困惑,我不知道我做错了什么。任何帮助将不胜感激。

编辑:

如果我添加:

<item name="colorPrimaryDark">@color/primary_dark</item>

实心状态栏变色:

另一个奇怪的事情是导航抽屉超出了该死的栏:

【问题讨论】:

  • 应该可以。如果您遗漏了什么,请看这里 -- stackoverflow.com/questions/19732972/…
  • 你在物理设备上试过了吗?
  • @Tasos 其实没什么可错过的。我已经读了上千遍了。
  • @V_J 是的,我在问题中提到过。
  • 尝试删除,true。让我知道结果。

标签: java android statusbar translucency android-statusbar


【解决方案1】:

这对我有用,在 styles-v21.xml 中添加:

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

android.support.v4.widget.DrawerLayout下的activity_main.xml中:

    android:android:fitsSystemWindows="true">

styles-v21.xml

<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

styles.xml

<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

activity_main.xml

<android.support.v4.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_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <include
            android:id="@id/app_bar"
            layout="@layout/app_bar" />

        <FrameLayout
            android:id="@id/container_body"
            android:layout_width="fill_parent"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </FrameLayout>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>

app_bar.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>

【讨论】:

  • 您能否为您的代码提供更多解释?它可能对 OP 或未来的用户有更多帮助。
【解决方案2】:

我终于找到了问题的根源。我正在使用MaterialDrawer 库为我的活动添加一个抽屉。当我删除它时,一切正常。我将发布另一个问题来发布此问题,并将在编辑中添加指向该问题的链接。

【讨论】:

  • 我假设你使用我的抽屉有点不对劲。因为半透明状态栏由抽屉自动处理。您甚至可以查看示例应用程序。我真的很想对此进行更多调查,因为您的回答表明图书馆是问题所在
  • 嗨@mikepenz。请查看我现在发布的问题:stackoverflow.com/questions/32818363/…
猜你喜欢
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 2016-03-20
  • 2016-04-18
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多