How to make Toolbar transparent?

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

It is self Q&A post I have transparent ActionBar which overlays layout. After migration to the latest support library I forced to rid off action bar by tool bar. The old ways to make it transparent and overlay that layout doesn't work

回答1:

All you need to is to define theme which hide action bar, define action bar style with transparent background and set this style to the toolbar widget. Please note that toolbar should be drawen as last view (over all view tree)

Layout:

Toolbar layout:



回答2:

Create your toolbar.xml file with background of AppBarLayout is @null

and here is result:



回答3:

The Toolbar works like a View, so the answer it's very simple.

toolbar.getBackground().setAlpha(0); 


回答4:

Checking Google's example Source Code I found out how to make the toolbar completely transparent. It was simpler than I thought. We just have to create a simple Shape drawable like this.

The name of the drawable is toolbar_bg

And then in the fragment or activity.. Add the toolbar like this.

    

And here we will have a fully transparent toolbar.

Don't add the if you do, this won't work.

Note: If you need the AppBarLayout, set the elevation to 0 so it doesn't draw its shadow.



回答5:

Add the following line in style.xml

Now add the following line in style-v21,

  

and set the theme as android:theme="@style/AppTheme"

It will make the status bar transparent.



回答6:

Just use the following xml code:

Code for the layout:

And add the following code in the colors.xml:

#00FFFFFF

This will give you the desired output.



回答7:

I implemented translucent Toolbar by creating two Theme.AppCompat.Light.NoActionBar themes and setting colorPrimary attribute to transparent color.

1) Create two themes Create one theme with for opaque Toolbar:

Second theme for transparent/overlay Toolbar:

2) In your activity layout, put Toolbar behind content so it can be displayed in front of it:

3) Apply the transparent theme to your acivity in AndroidManifest.xml

    


回答8:

Perhaps you need to take a look at this post transparent Actionbar with AppCompat-v7 21

Points that the post suggest are

  1. Just ensure that you use RelativeLayout to lay Toolbar and the body.
  2. Put the Toolbar in the last.
  3. Put transparent color for android:background attribute of the Toolbar

This should solve the problem without too much hassle.



回答9:

The simplest way to put a Toolbar transparent is to define a opacity in @colors section, define a TransparentTheme in @styles section and then put these defines in your toolbar.

@colors.xml

#33000000

@styles.xml

@activity_main.xml

That's the result:

Screenshot transparent toolbar



回答10:

Just add android:background="@android:color/transparent" like below in your appbar layout

` 


回答11:

I know am late for the party. I've created a simple class to manage the Toolbar transparency.

import android.annotation.SuppressLint; import android.graphics.drawable.ColorDrawable; import android.support.v7.widget.Toolbar;    public class TransparentToolbarManager {      private Toolbar mToolbar;     private ColorDrawable colorDrawable;     public static final int MAX_ALPHA = 255, MIN_ALPHA = 0;      public TransparentToolbarManager(Toolbar mToolbar) {         this.mToolbar = mToolbar;         this.colorDrawable = new ColorDrawable(mToolbar.getContext().getResources().getColor(R.color.colorPrimary));     }      public TransparentToolbarManager(Toolbar mToolbar, ColorDrawable colorDrawable) {         this.mToolbar = mToolbar;         this.colorDrawable = colorDrawable;     }      //Fading toolbar     public void manageFadingToolbar(int scrollDistance) {         if (mToolbar != null && colorDrawable != null) {             //FadeinAndOut according to the horizontal scrollValue             if (scrollDistance = MIN_ALPHA) {                 setToolbarAlpha(scrollDistance);             } else if (scrollDistance > MAX_ALPHA) {                 setToolbarAlpha(MAX_ALPHA);             }         }     }       @SuppressLint("NewApi")     public void setToolbarAlpha(int i) {         colorDrawable.setAlpha(i);         if (CommonHelper.isSupport(16)) {             mToolbar.setBackground(colorDrawable);         } else {             mToolbar.setBackgroundDrawable(colorDrawable);         }     }   } 

and the CommonHelper.isSupport()

public static boolean isSupport(int apiLevel) {         return Build.VERSION.SDK_INT >= apiLevel; } 


回答12:

try below code

style.xml



回答13:

goto res package and open color.xml set color primary to #00000000 its done.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!