简体   繁体   English

Android棒棒糖中的透明actionBar和statusBar

[英]Transparent actionBar and statusBar in Android lollipop

I am trying to create this interface : 我正在尝试创建此接口:

在此输入图像描述

And here is my actual result : 这是我的实际结果:

在此输入图像描述

  • The status bar is well transparent and we see my image as background : OK 状态栏透明,我们将图像视为背景:好的
  • The action bar is not transparent : NOK 操作栏不透明:NOK

Here is the code of my theme that I use for this activity : 以下是我用于此活动的主题代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- inherit from the material theme -->
    <style name="MaterialAppDetailTheme" parent="android:Theme.Material.Light">

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

        <!-- enable window content transitions -->
        <item name="android:windowContentTransitions">true</item>

        <!-- specify shared element transitions -->
        <item name="android:windowSharedElementEnterTransition">
            @transition/change_image_transform</item>
        <item name="android:windowSharedElementExitTransition">
            @transition/change_image_transform</item>

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

    </style>
</resources>

You can change your toolbar color to transparent like this: 您可以将工具栏颜色更改为透明,如下所示:

mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent));

You can change it's background on the XML too: 您也可以在XML上更改它的背景:

android:background="@android:color/transparent"

Or if you're using ActionBar: 或者,如果您正在使用ActionBar:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

Use getActionBar() if you're not using ActionBarActivity 如果您没有使用ActionBarActivity请使用getActionBar()

Result: 结果:

在此输入图像描述

Use this style: 使用这种风格:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:textColorPrimary">@color/my_text_color</item>
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="windowActionBarOverlay">true</item>
</style>

For API that >= 21, add to theme these lines of code 对于> = 21的API,将这些代码行添加到主题

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

Don't forget to add 别忘了添加

android:fitsSystemWindows="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM