更改ImageButton的形状颜色

时间:2016-02-14 22:14:31

标签: android

我有一个recyclerView,我创建了12个ImageButtons。 默认情况下,它们都是黑色的,因为我为imageButton制作了一个黑色纯色的自定义形状。自定义形状设置为imageButtons背景

形状:

 public static List<ColorButton> initColorButtons(){

    colorButtonList = new ArrayList<>();

    //here we retrive all colors from color.xml
    Resources resources = App.getAppContext().getResources();
    String colors[] = resources.getStringArray(R.array.backgroundcolors);

        for(int i=0; i<colors.length; i++){

        //Creates 12 ImageButtons with a custom shape
        colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i]));


        //Here each imagebutton should get its own color.
        Drawable drawable = colorButtonList.get(i).getButton().getBackground();

        if (colorButtonList.get(i).getButton().getBackground() instanceof GradientDrawable) {

            GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
            gd.setColor(Color.parseColor(colors[i]));

    }
}
    return colorButtonList;
}

这就是ImageButtons现在的样子。但它们都有相同的颜色,这不是我想要的。

enter image description here

我想动态地为每个ImageButton提供与color.xml中的color-array不同的颜色。我已经尝试了很多关于如何改变形状的纯色的解决方案,但没有一种方法有效。

为recyclerView创建12个imageButton的方法:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/colorbutton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="7dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:background="@drawable/bbtn">

</ImageButton>

为recyclerView创建的Imagebutton。 @ drawable / bbtn形状backgrundcolor默认设置为黑色

  <string-array name="backgroundcolors">
    <item>#000000</item>
    <item>#ffffff</item>
    <item>#373737</item>
    <item>#e6e6e6</item>
    <item>#EAE1D8</item>
    <item>#fd79a1</item>
    <item>#E849A1</item>
    <item>#ff0f68</item>
    <item>#c22032</item>
    <item>#F7E84E</item>
    <item>#0d4b7e</item>
    <item>#329de7</item>
    <item>#68be3f</item>
    <item>#006c35</item>
    <item>#395a4f</item>
    </string-array>

这是颜色数组。每个按钮应具有与列表中的一个不同的背景颜色:

public class ColorButton{

private ImageButton button;
private String color;
public static List<ColorButton> colorButtonList;


public ColorButton(ImageButton button, String color) {
    this.button = button;
    this.color = color;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public ImageButton getButton() {
    return button;
}

@SuppressLint("NewApi")
public static List<ColorButton> initColorButtons(){
    colorButtonList = new ArrayList<>();

    //here we retrive all colors from color.xml
    Resources resources = App.getAppContext().getResources();
    String colors[] = resources.getStringArray(R.array.backgroundcolors);

    for(int i=0; i<colors.length; i++){
        //Creates 12 ImageButtons with a custom shape
        colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i]));

        //Here each imagebutton should get its own color.
        Drawable drawable = colorButtonList.get(i).getButton().getBackground();

        if (drawable instanceof GradientDrawable) {
            GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
            gd.setColor(Color.parseColor(colors[i]));
        } else if (drawable instanceof RippleDrawable) {
            RippleDrawable rd = (RippleDrawable) drawable;
            // keep in mind that colors[i] should be a string with the hex representation of a color, like: #F4F4F4
            int color = Color.parseColor(colors[i]);
            rd.setColor(newColorStateList(color));
        }
    }
    return colorButtonList;
}


private static ColorStateList newColorStateList(int color) {
    int[][] states = new int[][] {
            new int[] { android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_enabled}, // disabled
    };

    int[] colors = new int[] {
            color, color
    };

    return new ColorStateList(states, colors);
}

这是我创建ImageButtons的类

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    //inflates the custom layout for a button
    View colorButtonView = inflater.inflate(R.layout.item_colorbutton, parent, false);

    //Return a new holder instance of a colorButton
    ViewHolder viewHolder = new ViewHolder(colorButtonView);
    return viewHolder;
}

}

这是来自我的recyclerView Adapter类,我在其中渗透layout.item_colorbutton。 &lt; - 这是这篇文章的顶部

{{1}}

1 个答案:

答案 0 :(得分:2)

您的问题是,在您的情况下getBackground()正在返回RippleDrawable,而不是GradientDrawable。两者都扩展了Drawable,但你不能将其中一个扩展到另一个。 试试这个:

public List<ImageButton> initColorButtons(){
    colorButtonList = new ArrayList<>();

    //here we retrive all colors from color.xml
    Resources resources = App.getAppContext().getResources();
    String colors[] = resources.getStringArray(R.array.backgroundcolors);

    for(int i=0; i<colors.length; i++){
        //Creates 12 ImageButtons with a custom shape
        colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i]));

        //Here each imagebutton should get its own color.
        Drawable drawable = colorButtonList.get(i).getBackground();

        if (drawable instanceof GradientDrawable) {
            GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
            gd.setColor(Color.parseColor(colors[i]));
        } else if (drawable instanceof RippleDrawable) {
            RippleDrawable rd = (RippleDrawable) drawable;
            // keep in mind that colors[i] should be a string with the hex representation of a color, like: #F4F4F4
            int color = Color.parseColor(colors[i]);
            rd.setColor(newColorStateList(color));
        }
    }
    return colorButtonList;
}

private ColorStateList newColorStateList(int color) {
    int[][] states = new int[][] {
            new int[] { android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_enabled}, // disabled
    };

    int[] colors = new int[] {
            color, color
    };

    return new ColorStateList(states, colors);
}

结果:

screenshot

您可以阅读有关如何在this SO question中创建ColorStateList的更多信息。

编辑:为了正确修改您使用方法onCreateViewHolder创建的按钮,您需要在适配器中实施onBindViewHolder并更改按钮的颜色

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
        Drawable drawable = holder.getBackground();

        if (drawable instanceof GradientDrawable) {
            GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
            gd.setColor(Color.parseColor(colors[position]));
        } else if (drawable instanceof RippleDrawable) {
            RippleDrawable rd = (RippleDrawable) drawable;
            int color = Color.parseColor(colors[position]);
            rd.setColor(newColorStateList(color));
        }
}