更改按钮形状背景颜色

时间:2014-07-02 08:13:52

标签: android button view colors background

我尝试创建某种自定义的圆角按钮,例如this answer。 但是我的GridView需要很多(大约18个),并且所有这些都应该有不同的背景颜色。

不幸的是,view.setBackgroundColor(int c)不仅会更改自定义按钮的颜色,还会将形状更改为(因此我的圆角会消失。

那么,有没有办法只改变视图的背景颜色(形状的颜色)而不改变它的形状? (创造18种不同的xmls并不是最好的方式)

addapter的完整代码:

PaletteView view = (PaletteView) ((convertView == null) ? mInflater.inflate(R.layout.palette_item, null)
            : convertView);

    view.findViewById(R.id.rounded).setBackgroundColor(mColors[position]);

    return view;

2 个答案:

答案 0 :(得分:0)

没时间测试,但也许......

将ID(shape_id)添加到您希望在drawable中更改颜色的项目中。 然后使用以下

 LayerDrawable bgDrawable = (LayerDrawable) view.getBackground();
 GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.shape_id);
 shape.setColor(mColors[position]);

https://stackoverflow.com/a/16637696/3309883

答案 1 :(得分:0)

所以,我找到了解决问题的方法 - 创建形状/选择器programmaticaly。

现在,我只得到了一个只有形状的rounded_shape.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<corners android:radius="@dimen/rounded_radius" />

<stroke android:width="1dip" />

并创建了静态类,用于为按钮创建完整的选择器:

public class DrawableUtils {

/**
 * Blend {@code color1} and {@code color2} using the given ratio.
 * 
 * @param ratio
 *            of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend, 0.0 will return
 *            {@code color2}.
 */
private static int blendColors(int color1, int color2, float ratio) {
    final float inverseRation = 1f - ratio;
    float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
    float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
    float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
    return Color.rgb((int) r, (int) g, (int) b);
}

/**
 * Create selector with specified {@code mainColor} (setting darkened {@code mainColor} for state_pressed)
 * 
 * @param mainColor
 *            color of the view
 */
public static StateListDrawable getSelector(Context c, int mainColor) {
    StateListDrawable selector = new StateListDrawable();
    /*
     * CAREFUL! We NEED to use 2 different objects instead of using 1 with different setColor(...) calls
     */
    GradientDrawable stateSimple, statePressed;

    // Getting pre-created shape
    statePressed = (GradientDrawable) c.getResources().getDrawable(R.drawable.rounded_shape);
    stateSimple = (GradientDrawable) c.getResources().getDrawable(R.drawable.rounded_shape);

    // Setting color for state_pressed
    statePressed.setColor(blendColors(mainColor, c.getResources().getColor(R.color.tinted_black), 0.5f));
    selector.addState(new int[] { android.R.attr.state_pressed }, statePressed);
    selector.addState(new int[] { -android.R.attr.state_enabled }, statePressed);   // "-" is for "false"

    // Setting color for main state
    stateSimple.setColor(mainColor);
    selector.addState(new int[] { android.R.attr.state_enabled }, stateSimple);

    return selector;
}

}

最后,从GridView适配器调用它:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        view.findViewById(R.id.rounded).setBackgroundDrawable(DrawableUtils.getSelector(mC, mColors[position]));
    } else {
        view.findViewById(R.id.rounded).setBackground(DrawableUtils.getSelector(mC, mColors[position]));
    }