【问题标题】:Linear Layout Background image set alpha线性布局背景图像集 alpha
【发布时间】:2012-06-23 15:58:07
【问题描述】:

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

【问题讨论】:

  • 你能把XML代码粘贴到你定义背景的地方吗?您是指向可绘制对象还是直接指向图像?
  • 请将代码放在您的问题中,而不是作为评论。
  • 对不起,我做不到...重要的一行是 android:background="@drawable/ic_launcher"
  • 我明白了......好吧,我会尝试添加一个答案,也许会有所帮助。
  • 非常感谢您的宝贵努力。

标签: android


【解决方案1】:

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

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

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

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

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

在activity中给布局:

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);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    相关资源
    最近更新 更多