2011-06-27 24 views
0

此前,我能夠動態創建一個android.widget.Button,其背景色通過Button的背景圖像的透明部分可見。我無法重新創建它。我曾嘗試過:如何通過透明背景圖像創建背景顏色可見的按鈕?

Button button = (Button) findViewById(id.button1); 
try { 
    button.setBackgroundColor(Color.RED); 
    Bitmap bm = BitmapFactory.decodeStream(getAssets().open("transparent.png")); 
    button.setBackgroundDrawable(new BitmapDrawable(bm)); 
    button.invalidate(); 
} catch (IOException e) {  
    throw new RuntimeException(e); 
} 

只有圖像是可見的,如果我運行上述。如果我將調用移動到setBackgroundDrawable調用下方的setBackgroundColor,則只能看到紅色,而不是圖像。我該如何做到這一點,以便我可以看到圖像,並通過其透明部分看到背景?

回答

0

首先它使用一個ImageButton比較容易,它有兩層,一個是背景,另一個是圖像頂部。 設置你的背景顏色與button.setBackgroundColor

然後設置在頂部的透明圖像:button.setImageDrawable(getResources().getDrawable(R.drawable.transparent));或設置SRC preperty在XML

使用按鈕填充設置調整應該多少底色顏色相配的圖像周圍。

+0

謝謝!這樣做(儘管我仍然想知道如何用普通的舊Button實現它)。 –

+0

如果你做了,也許做一些非常相似的事情,我還沒有嘗試;) – Lumis