2012-11-29 50 views
1

我的一項活動中有一組按鈕。按鈕看起來很好,因爲它們來自Android系統(灰色),因爲我的活動背景是灰色的。我試過以下如何更改標準系統按鈕的背景透明度

button.setBackgroundColor(Color.argb(125, 200, 200, 200)); 

它創建所需的透明度,但我失去的按鈕狀態等按壓,高達等

我怎能與Android的船隻能降低不透明度按鈕的狀態?

回答

2

在你的類,使用下面的代碼:

alpha = new AlphaAnimation(0.3F, 0.3F); //Set opacity - Range 0.0 to 1.0 
alpha.setDuration(0); // Set animation duration 
alpha.setFillAfter(true); // Maintaining the effect to the button 
yourButton.startAnimation(alpha); 

我希望我幫助。

再見!

+0

@Tanyna Bonaldo是的,你做了幫助,真棒夥伴,只是我想要的感謝;) – turtleboy

0

在XML用於期望的按鈕,則應該僅僅是能夠把:

機器人:背景=:

我用這個沒有默認顯示可拉伸「@機器人彩色/透明」灰色背景。

-cheers

+0

警告這將放棄所有的背景...有甚至不會是因爲大綱整個按鈕圖像實際上是在後臺 – edofic

0

動態方式:

Button.getBackground(),如果你設置一個位圖作爲背景,或ColorDrawable如果你設置的顏色作爲背景,通常它會返回一個BitmapDrawable。

ColorDrawable colorDrawable =(ColorDrawable)button.getBackground();
colorDrawable.setAlpha(125);

這將工作正常。

靜態方式:
佈局 ,設置背景顏色一樣#7EC8C8C8

+0

嗨,它確實有效,但它是使用動畫更改不透明度的最佳方式?我認爲如果你改變可繪製背景的Alpha通道會更好。 – qiuping345