2010-11-27 24 views
3

我正在開發一個簡單的應用程序,因爲有6個按鈕的設置。 當我點擊其中一個按鈕時,其他按鈕必須部分透明。我試圖做到這一點通過設置的5個按鈕按鈕的透明度不會改變使用'setAlpha'

acre_button.getBackground().mutate().setAlpha(155);

應用程序的UI如我所料不改變阿爾法。我只得到了3出5得到transparent.when點擊兩個按鈕,它正在慢慢改變它的透明度

在此先感謝

問候, kariyachan

+0

那麼問題是什麼? – 2010-11-27 09:49:15

+0

我爲按鈕設置了α155。某些按鈕背景圖像透明度不變(UI不會相應地改變) – DroidBot 2010-11-27 10:16:11

回答

0

嘗試以下操作:

button.setBackgroundColor(android.R.color.transparent); 
0

在此處查找android-sdk目錄中的按鈕背景:android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png

您可以修改它使其半透明(請注意,這是一個9-patch,你不應該改變黑線的不透明度)。

一旦有了這種變化的按鈕在你的繪製目錄,你可以添加到您的代碼:

button.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparentImage)); 

,使其半透明和

button.setBackgroundDrawable(getResources().getDrawable(Android.R.drawable.btn_default_normal)); 

改回來。

1
Button btn; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    btn = (Button) findViewById(R.id.main_btn); 
    Drawable d = getResources().getDrawable(R.drawable.imagen); 
    d.setAlpha(60); 
    btn.setBackgroundDrawable(d); 
} 

這對我的作品:)

0

對於任何一個解決方案仍在追捕本:

方法setBackgroundDrawable(Drawable d)棄用API 16

假設你按鈕的ID是buttonId,並且您的繪圖名爲button_img, 處理此在onCreate方法中使用以下內容:

((Button)(findViewById(R.id.buttonId))).setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Drawable d = v.getResources().getDrawable(R.drawable.button_img); 
      d.setAlpha(40); 
      if (Build.VERSION.SDK_INT >= 16) 
       v.setBackground(d); 
      else 
       v.setBackgroundDrawable(d); 
      //Then call your next Intent or desired action. 

     } 
    }); 

經過測試,適合我!