2011-12-21 77 views
17

嗨我有一個佈局,我用來填補我的網頁,我必須設置一個背景圖像保存在我的drawable folder在該佈局。Android佈局背景阿爾法

我想然後將圖像的alpha value設置爲相當低的值,幾乎使圖像像水印一樣。

我的XML看起來像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/background" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="@drawable/main_background" > 

,你可以看到我已經分配了id佈局

我想在我的OnCreate我可以做這樣的事情?

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

這不但是工作,我懷疑它,因爲我想投背景的View我應該怎麼投它?

回答

36

嘗試使用:

Drawable.setAlpha(); 

你應該做這樣的事情:

View backgroundImage = findViewById(R.id.background); 
Drawable background = backgroundImage.getBackground(); 
background.setAlpha(80); 
+2

雖然這種方法可行,它出現要獲得原始狀態或100%alpha,您可能需要使用0-1000的比例。所以如果你想讓一個按鈕看起來是50%的alpha,你可能實際上必須設置Alpha(200)而不是setAlpha(50)...至少在按鈕上使用API​​ 19 ...所以重置按鈕回到它的原始外觀,我不得不設置Alpha(1000)... – whyoz 2014-01-30 02:01:59

+3

100%不透明度等於255,而不是1000 – snapix 2014-10-15 08:54:56

33

如果你想設置的XML阿爾法然後ü可以試試這個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/background" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="#CC000000" > 

前兩位數字用於設置阿爾法在這裏阿爾法是80%接下來的6位數字顏色代碼爲

因此,alpha設置爲十六進制編碼。 FF = 100%和00 = 0%,因此80%不是十六進制的80,更多標準值見this後。

1

您可以爲可繪製的視圖設置Alpha而不是視圖!你可以得到背景的繪製和這樣做:

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

使用這對你的LinearLayout

的android:背景= 「#92000000」