视图中背景可绘制图像的不透明度(使用 XML 布局)

发布于 2024-10-16 20:42:27 字数 372 浏览 3 评论 0原文

我只是想知道是否有办法改变 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骷髅 2024-10-23 20:42:27

我最终选择了编程解决方案,因为它看起来无法通过 XML 布局来完成。

Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);

// setting the opacity (alpha)
rightArrow.setAlpha(10);

// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);

I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.

Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);

// setting the opacity (alpha)
rightArrow.setAlpha(10);

// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);
风吹过旳痕迹 2024-10-23 20:42:27

这可能会使您的工作更简单

View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);

Alpha 值 0-255,0 表示完全透明,255 表示完全不透明,

来自:此答案

This might make your Work simpler

View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);

Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque

from: This Answer

慵挽 2024-10-23 20:42:27

您还可以使用 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

掩饰不了的爱 2024-10-23 20:42:27

您可以将图像嵌入 xml 中,这样您就可以在图形布局中看到它

<LinearLayout
        style="@style/LoginFormContainer"
        android:id="@+id/login_layout"
        android:orientation="vertical" 
        android:background="@drawable/signuphead">

并更改代码,如下所示以使其透明:

Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);

You can embed the image in xml, so you'll be able to see it in the Graphical Layout

<LinearLayout
        style="@style/LoginFormContainer"
        android:id="@+id/login_layout"
        android:orientation="vertical" 
        android:background="@drawable/signuphead">

And change the code like this to make it transparent:

Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);
风启觞 2024-10-23 20:42:27

您给出的答案没有完全回答您提出的问题。这就是我所做的。

    Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
    login_activity_top_background.setAlpha(127);
    LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
    login_activity_top.setBackgroundDrawable(login_activity_top_background);

The answer you gave didn't exactly answer the question you asked. Here's what I did.

    Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
    login_activity_top_background.setAlpha(127);
    LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
    login_activity_top.setBackgroundDrawable(login_activity_top_background);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文