在运行时按颜色创建按钮背景

时间:2016-01-19 17:27:20

标签: android

在我的应用中,我有下面的选择器。

是否可以通过颜色而不是图像绘制来创建类似的选择器,但是在运行时?

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item 
        android:state_window_focused="false" 
        android:drawable="@drawable/ic_button_orange_normal" />

    <item  
        android:state_pressed="true" 
        android:drawable="@drawable/ic_button_orange_pressed" />

    <item 
        android:state_focused="true" 
        android:drawable="@drawable/ic_button_orange_focused" />

    <item 
        android:drawable="@drawable/ic_button_orange_normal" />

</selector>

我已尝试使用下面的函数在运行时创建选择器,但我需要创建一个圆形按钮,而不是方形。

这可能吗?

private StateListDrawable makeSelector(int color)
{
  StateListDrawable res = new StateListDrawable();
    res.setExitFadeDuration(400);
    res.setAlpha(45);
    ShapeDrawable d = new ShapeDrawable();
    res.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(ColorUtilities.decrease(color, 0x003030)));
    res.addState(new int[] {}, new ColorDrawable(Color.TRANSPARENT));
    return res;   
}

再次编辑......我找到了创建圆形选择器的方法......

private static ShapeDrawable getRoundShapeDrawable(int color)     
{
  ShapeDrawable shape_drawable = new ShapeDrawable(new OvalShape());
  shape_drawable.getPaint().setColor(color);
  return shape_drawable;  
}

public static StateListDrawable getRoundShapeSelector(int normal_color, int pressed_color)
{
  ShapeDrawable normal_shape_drawable = getRoundShapeDrawable(normal_color), pressed_shape_drawable = getRoundShapeDrawable(pressed_color);
  StateListDrawable state_list_drawable = new StateListDrawable();
  state_list_drawable.addState(new int[] { android.R.attr.state_pressed }, pressed_shape_drawable);
  state_list_drawable.addState(new int[] { android.R.attr.state_focused }, pressed_shape_drawable);
  state_list_drawable.addState(new int[] { }, normal_shape_drawable);
  return state_list_drawable;   
}

2 个答案:

答案 0 :(得分:1)

您可以使用&#34; StateListDrawable&#34;

StateListDrawable

并在其中添加状态。 如果你在实施这个问题时遇到问题,请告诉我。

答案 1 :(得分:0)

如果要创建圆形按钮。然后你可以使用layer-list然后将这个drawable xml添加为Layer。

EG。如何创建圆形按钮。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#fff" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#d3d3d3" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:left="2dp"
        android:right="1dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#EBEBEB" />
        </shape>
    </item>
    <item
        android:id="@+id/wl_btn_bg_shape"
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp">
        <shape android:shape="rectangle" >
            <corners android:radius="40dp" />
            <solid android:color="#00caa8" />
        </shape>
    </item>

</layer-list>

在drawable中创建并保存代码为wl_btn_bg.xml

现在您需要将此xml添加为视图背景

LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
view.setBackground(layers);

如果您想更改选择器的颜色,则需要在图层中添加以下行

LayerDrawable layers = (LayerDrawable) context.getResources().getDrawable(R.drawable.wl_btn_bg);
        GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.wl_btn_bg_shape));
shape.setColor(Color.parseColor("#ff6655"));

完成....

相关问题