Android Material Design Transparent ActionBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26558705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:48:55  来源:igfitidea点击:

Material Design Transparent ActionBar

androidandroid-actionbarandroid-5.0-lollipop

提问by user3184899

I'd like to know if anyone knows how to make an activity with transparent action bar, like the one you have in the new Google Play Store when you go to an app's page.

I'd like to know if anyone knows how to make an activity with transparent action bar, like the one you have in the new Google Play Store when you go to an app's page.

I don't care about the scrolling and turning from transparent into solid color background, I just need the action bar transparent.

I don't care about the scrolling and turning from transparent into solid color background, I just need the action bar transparent.

回答by Thiago

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

This above will result exception on Lollipop devices. colorPrimary must be opaque.

This above will result exception on Lollipop devices. colorPrimary must be opaque.

Stylish your actionbar using style:

Stylish your actionbar using style:

<style name="ThemeActionBar"
        parent="Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:background">@null</item>
        <!-- Support library compatibility -->
        <item name="background">@null</item>
</style>

And in your theme, just include:

And in your theme, just include:

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

回答by Cristian Gomez

You just need to put <item name="colorPrimary">@android:color/transparent</item>and set windowActionBarOverlayto true on your app theme like this:

You just need to put <item name="colorPrimary">@android:color/transparent</item>and set windowActionBarOverlayto true on your app theme like this:

<resources>

    <!-- Base application theme. -->
    <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>

</resources>

Final result should look like this:

Final result should look like this:

final result of code

final result of code

回答by Mohit Charadva

1) Set AppBarLayoutelevation property to 0dp.

1) Set AppBarLayoutelevation property to 0dp.

app:elevation="0dp"

app:elevation="0dp"

2) Set Toolbarbackground color to transparent.

2) Set Toolbarbackground color to transparent.

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

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

Whole xml will look like below:

Whole xml will look like below:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">

    <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/AppTheme.PopupOverlay">

        ......

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

enter image description here

enter image description here

回答by Maksudul Hasan Raju

Transparent Actionbar

Transparent Actionbar

values/styles.xml:

values/styles.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
...
</style>

<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="colorPrimary">@android:color/transparent</item>
</style>

<style name="AppTheme.ActionBar" parent="AppTheme">
    <item name="windowActionBarOverlay">false</item>
    <item name="colorPrimary">@color/default_yellow</item>
</style>

values-v21/styles.xml:

values-v21/styles.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
    ...
</style>

<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
    <item name="colorPrimary">@android:color/transparent</item>
</style>

<style name="AppTheme.ActionBar" parent="AppTheme">
    <item name="colorPrimaryDark">@color/bg_colorPrimaryDark</item>
    <item name="colorPrimary">@color/default_yellow</item>
</style>

use these themes in your AndroidManifest.xmlto specify which activities will have a transparent or colored ActionBar

use these themes in your AndroidManifest.xmlto specify which activities will have a transparent or colored ActionBar

<activity
            android:name=".MyTransparentActionbarActivity"
            android:theme="@style/AppTheme.ActionBar.Transparent"/>

    <activity
            android:name=".MyColoredActionbarActivity"
            android:theme="@style/AppTheme.ActionBar"/>

回答by kreker

enter image description here

enter image description here

<style name="AppTheme" parent="Theme.MaterialComponents">
    <item name="android:navigationBarColor" tools:targetApi="lollipop">@color/color_primary
    </item>
    <item name="android:statusBarColor" tools:targetApi="lollipop">@color/color_primary</item>
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primary_dark</item>
    <item name="colorAccent">@color/color_accent</item>
</style>

<style name="MainTheme" parent="AppTheme">
    <item name="actionBarStyle">@style/MyTheme.ActionBar</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

<style name="MyTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="elevation" tools:targetApi="lollipop">0dp</item>
    <item name="background">@color/semi_transparent</item>
</style>
</resources>


<resources>
<color name="color_primary">#212121</color>
<color name="color_primary_dark">@android:color/black</color>
<color name="color_accent">#4285F4</color>
<color name="semi_transparent">#66000000</color>
</resources>

回答by John Joe

This worked for me

This worked for me

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);   
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

回答by Roman Nazarevych

There is also way to do it pragmatically.

There is also way to do it pragmatically.

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
mActionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mActionBar.setElevation(0);