如何使支持工具栏背景透明?

时间:2022-08-27 12:16:08

Is there a way to make the new Android support Toolbar:

是否有办法使新的Android支持工具栏:

android.support.v7.widget.Toolbar

Have a transparent background?

有透明的背景吗?

I tried setting the colorPrimary to an ARGB (#00ffffff) but it just gives me a solid gray bar. And Toolbar only allows me to set a background drawable. Is it kosher to set the background drawable to a transparent PNG, or is there a better way to do this?

我尝试将colorPrimary设置为ARGB (#00ffff),但它给了我一个坚实的灰色条。工具栏只允许我设置背景绘制。将背景绘制设置为透明的PNG是否更合适,或者是否有更好的方法?

Ideally I'd be able to do it in code / animate it so certain activities have a transparent bar and some activities don't.

理想情况下,我可以用代码/动画的方式来实现它,所以某些活动有一个透明的栏,而有些活动没有。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


17  

You can either set the background to android's default transparent color, which works just fine. Add this to the layout you want a transparent Toolbar:

你可以将背景设置为android默认的透明颜色,效果很好。将此添加到要透明工具栏的布局中:

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

android:背景= " @android:颜色/透明”

If you want to change the alpha programmatically, you can do it by modifying the alpha on the Toolbar background itself. Just get an instance of the Drawable and set the alpha there:

如果希望通过编程方式更改alpha,可以通过修改工具栏背景上的alpha来实现。只需要获取一个可绘制的实例并在那里设置alpha:

mToolbar = findViewById(R.id.my_toolbar);
mToolbar.getBackground().setAlpha(0);

#2


8  

To answer Nicholas Pesa's question and maybe some others in the future you could set the alpha with a float by using a ratio, something like.

为了回答Nicholas Pesa的问题,也许将来还会有一些其他的,你可以用一个比率来设定一个浮点数,比如。

mToolbar.getBackground().setAlpha((int)(myFloatValue) * 255);

#1


17  

You can either set the background to android's default transparent color, which works just fine. Add this to the layout you want a transparent Toolbar:

你可以将背景设置为android默认的透明颜色,效果很好。将此添加到要透明工具栏的布局中:

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

android:背景= " @android:颜色/透明”

If you want to change the alpha programmatically, you can do it by modifying the alpha on the Toolbar background itself. Just get an instance of the Drawable and set the alpha there:

如果希望通过编程方式更改alpha,可以通过修改工具栏背景上的alpha来实现。只需要获取一个可绘制的实例并在那里设置alpha:

mToolbar = findViewById(R.id.my_toolbar);
mToolbar.getBackground().setAlpha(0);

#2


8  

To answer Nicholas Pesa's question and maybe some others in the future you could set the alpha with a float by using a ratio, something like.

为了回答Nicholas Pesa的问题,也许将来还会有一些其他的,你可以用一个比率来设定一个浮点数,比如。

mToolbar.getBackground().setAlpha((int)(myFloatValue) * 255);