2

我在我的android程序中使用图像作为线性布局的背景。我需要为此设置不透明度。谁能告诉我我该怎么做?..半透明的图像不显示不透明度,不虽然知道原因。提前感谢您的宝贵意见

4

1 回答 1

6

如果您以编程方式设置 LinearLayout 的背景,这应该没有任何问题。

您正在寻找的是Drawable.setAlpha(int alpha)方法。来自个人项目:

ImageView image = (ImageView) row.findViewById(R.id.list_icon);
image.setImageResource(R.id.something);
image.setAlpha(110);

不完全一样,但也许你明白了。

您正在使用可绘制对象作为布局的背景。这里的挑战是获取代表您的可绘制对象的变量。这是在这里完成的:

在布局的活动中:

Resources res = getResources();
Drawable background = res.getDrawable(R.drawable.*the id*);
    // The layout which are to have the background:
LinearLayout layout = ((LinearLayout) findViewById(R.id.*you get it*));
    // Now that we have the layout and the background, we ajust the opacity 
    // of the background, and sets it as the background for the layout
background.setAlpha( *a number* );
layout.setBackgroundDrawable(background);

它应该工作。至少它确实对我有用。

于 2012-06-23T16:28:50.807 回答