以编程方式设置按钮背景将其从屏幕上删除

Setting button background programmatically removes it from screen

我正在尝试以编程方式更改按钮的背景颜色,但是当我更改其颜色时,按钮从屏幕上消失了。

这是布局中的按钮

<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));

但结果相同

试试

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

另一种方法是这样的:

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

也许您尝试将颜色更改为与 activity 的背景相同并且它消失了,因为它是相同的颜色...

尝试使用 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 <-);