2012-06-23 100 views
2

我在我的android程序中使用圖像作爲線性佈局的背景。我需要爲此設置不透明度。任何人都可以告訴我該如何做到這一點?半透明不顯示不透明度,雖然不知道原因。 在此先感謝您的寶貴意見線性佈局背景圖像集alpha

+0

您可以將XML代碼粘貼到您定義背景的位置嗎?你是指向一個drawable還是直接指向圖像? –

+0

請把你的問題的代碼,而不是評論。 –

+0

對不起我不能這樣做... 重要的線是android:background =「@ drawable/ic_launcher」 –

回答

6

如果以編程方式設置LinearLayout的背景,這應該不會有任何問題。

你在找什麼是Drawable.setAlpha(int alpha)方法。從個人項目:

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

不完全相同,但也許你明白了。

您正在使用drawable作爲佈局的背景。這裏面臨的挑戰是獲取代表繪圖的變量。這樣做是here

在活動佈局:

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

,它應該工作。至少它確實爲我工作。

+0

謝謝,很好的例子。 – Dimon