视图中背景可绘制图像的不透明度(使用 XML 布局)
我只是想知道是否有办法改变 View
(即 TextView
等)的背景图像的不透明度。
我知道我可以像这样设置背景图像:
android:background="@drawable/my_drawable_image"
或者我可以使用 alpha 设置来设置特定的背景颜色,如下所示:
android:background="#10f7f7f7"
如果我将背景设置为可绘制图像?我想在 XML 布局中执行此操作。我已经知道我可以抓取 Drawable 对象并以编程方式设置 alpha,但我想看看是否可以在布局中做到这一点。
I was just wondering if there was a way to change the opacity of the background image for a View
(ie. TextView
, etc.).
I know that I can set the background image like this:
android:background="@drawable/my_drawable_image"
Or I can set a specific background colour with an alpha setting like this:
android:background="#10f7f7f7"
Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最终选择了编程解决方案,因为它看起来无法通过 XML 布局来完成。
I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.
这可能会使您的工作更简单
Alpha 值 0-255,0 表示完全透明,255 表示完全不透明,
来自:此答案
This might make your Work simpler
Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque
from: This Answer
您还可以使用 XML 来更改透明度:
android:alpha = "0.7"
alpha 的值范围为 0 到 1
You can also use XML to change the transparency:
android:alpha = "0.7"
The value of alpha ranges from 0 to 1
您可以将图像嵌入 xml 中,这样您就可以在图形布局中看到它
并更改代码,如下所示以使其透明:
You can embed the image in xml, so you'll be able to see it in the Graphical Layout
And change the code like this to make it transparent:
您给出的答案没有完全回答您提出的问题。这就是我所做的。
The answer you gave didn't exactly answer the question you asked. Here's what I did.