Java 如何使支持工具栏背景透明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26574138/
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
How to make the support Toolbar background transparent?
提问by Keith
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 (#00ffffff),但它只给了我一个纯灰色条。并且Toolbar
只允许我设置背景可绘制。将可绘制的背景设置为透明的 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.
提前致谢。
采纳答案by ebarrenechea
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 的默认透明颜色,这很好用。将此添加到您想要透明的布局中Toolbar
:
android:background="@android:color/transparent"
android:background="@android:color/transparent"
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,可以通过修改Toolbar
背景本身的 alpha 来实现。只需获取 的实例Drawable
并在那里设置 alpha:
mToolbar = findViewById(R.id.my_toolbar);
mToolbar.getBackground().setAlpha(0);
回答by Jraco11
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 的问题以及将来可能的其他问题,您可以使用比率设置带有浮点数的 alpha,例如。
mToolbar.getBackground().setAlpha((int)(myFloatValue) * 255);