2015-05-15 39 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; 

也许你试着改变颜色一样的活动的背景是,它消失了,因为它是相同的颜色...

相关问题