0

我想将 setBounds() 用于图层列表中的特定项目,该项目用作按钮的背景。但我无法让 LayerDrawable 对象调用 findDrawableByLayerId()。

任何人都可以帮助解决这个问题吗?

4

2 回答 2

2

使用此方法在新的 layerdrawable 上创建 :) 我无法使用 findDrawableByLayerId() :(

@Override
public void setBackgroundDrawable(Drawable background) {
    Log.d("button","setting new background  ");
    Drawable[] layers = new Drawable[5];
    Resources resources = getResources();

    layers[0] = resources.getDrawable(R.drawable.outer_rectangle);
    layers[1] = resources.getDrawable(R.drawable.inner_rectangle);
    layers[2] = resources.getDrawable(R.drawable.upper_ovel);
    layers[3] = resources.getDrawable(R.drawable.gradient_fill);
    layers[4] = resources.getDrawable(R.drawable.lower_ovel);

    LayerDrawable layerDrawable = new LayerDrawable(layers);

    layerDrawable.setLayerInset(0, 0, 0, 0, 0);
    layerDrawable.setLayerInset(1, 3, 3, 3, 0);
    layerDrawable.setLayerInset(2, 3, 15, 3, 25);
    layerDrawable.setLayerInset(3, 3, 23, 3, 0);
    layerDrawable.setLayerInset(4, 4, 60, 4, -5);

    super.setBackgroundDrawable(layerDrawable);
}
于 2012-12-12T11:42:07.810 回答
1

如果你想使用findDrawableByLayerId(int id),你应该setId(int index, int id)首先做,如果不是,id总是-1。

于 2017-11-15T08:57:26.700 回答