我有一个ImageButton:
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/round_color"
android:layout_below="@+id/lamps_list_room"
android:layout_alignStart="@+id/lamps_list_room"
android:layout_marginStart="69dp"
android:layout_marginTop="62dp"
android:id="@+id/color_picker_button"
android:background="@color/colorPrimaryDark"/>
形状:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="100dp" />
<stroke
android:color="#FFFFFF"
android:width="2px">
</stroke>
</shape>
更改形状的纯色的最佳方法是什么?
ImageButton imageButton = (ImageButton) ((Activity) mContext).findViewById(R.id.color_picker_button);
Drawable drawable = imageButton.getBackground();
drawable.setColorFilter(0xff999999, PorterDuff.Mode.MULTIPLY);
形状仍然是白色......
编辑:
即使我尝试
Drawable drawable = mContext.getDrawable(R.drawable.round_color);
ColorFilter filter = new LightingColorFilter(Color.BLACK, Color.BLACK);
if (drawable != null) {
drawable.setColorFilter(filter);
}
它仍然无法运作!
答案 0 :(得分:0)
这是示例代码
GradientDrawable bgShape = (GradientDrawable)YourView.getBackground();
bgShape.setColor(Color.BLACK);
答案 1 :(得分:0)
我就这样做,相应地改变代码。
GradientDrawable bgShape = (GradientDrawable) getChildAt(0).getBackground();
bgShape.setColor();
答案 2 :(得分:0)
你可以试试这个:
GradientDrawable gradientDrawable= (GradientDrawable) color_picker_button.getDrawable();
gradientDrawable.setColor(Color.BLACK);