2015-05-15 40 views
-2

我試圖以編程方式更改按鈕的背景色,但是當我更改其顏色按鈕時從屏幕消失。設置按鈕背景編程從屏幕上將其刪除

這裏是佈局

<Button     
    android:id="@+id/ibtn_EA_ColorPick_new" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:background="@drawable/clr_btn" 
    /> 

按鈕,這裏是我如何改變其背景

btn_ColorPick.setBackgroundColor(btn_ColorPick.getContext().getResources().getColor(R.color.BlackColor)); 

我也曾嘗試

btn_ColorPick.setBackgroundColor(getResources().getColor(R.color.BlackColor)); 

但同樣的結果

+0

你有沒有嘗試刪除'機器人:背景= 「@繪製/ clr_btn」''從一個xml' d改爲添加一個'bacgroundColor =「@ android:colors/red」',然後嘗試如果相同的行爲 – hrskrs

+0

是您的活動背景並且按鈕顏色相同? –

回答

1

嘗試使用的ImageButton -

<ImageButton 
     android:id="@+id/ibtn_EA_ColorPick_new" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:src="@drawable/clr_btn" 
     android:background="@drawable/red_color" 
     /> 

btn_ColorPick.setBackgroundColor(->ur color <-); 
+0

將其轉換爲ImageButton工作。謝謝 –

0

嘗試

btn_ColorPick.setBackgroundColor(Color.RED); //import of android.graphics.Color 

另一種方法是這樣的:

bt_exButton.getBackground().setColorFilter(0xFFBBAA00, PorterDuff.Mode.MULTIPLY); //import od import android.graphics.PorterDuff; 

也許你試着改變顏色一樣的活動的背景是,它消失了,因爲它是相同的顏色...

相關問題