質問

I'm trying to update the background bounds of an EditText view such that the end result resembles something like this...

+----------------+
|  Empty Space   |
|                |
| +------------+ |
| | Background | |
| +------------+ |
+----------------+

My current approach is to obtain the background in onLayout and simply update the bounds...

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  super.onLayout(changed, left, top, right, bottom);
  ...
  getBackground().setBounds(newLeft, newTop, newRight, newBottom);
}

However, this doesn't work at all. The bounds are being applied, but when it draws, it doesn't change. The closest i've come to, is changing the bounds in onDraw, however, it will initially be drawn at it's original place, an then immediately be re-drawn to it's new position... How can I reliably change the background bounds?

役に立ちましたか?

解決

After some more research, the only way I was able to solve this, is to create an intermediary Drawable (man in the middle) and delegate all public methods to the actual Drawable. Then override setBounds to set whatever value I want...

public class MyCustomView extends EditText {

  @Override
  public void setBackground(Drawable background) {
    super.setBackground(new IntermediaryDrawable(background));
  }

  ...

  private class IntermediaryDrawable extends Drawable {
    private Drawable theRealDrawable;

    public IntermediaryDrawable(Drawable source) {
      theRealDrawable = source;
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
      theRealDrawable.setBounds(left, 100, right, bottom);
    }

    ...
  }
}

Pretty hacky. If anyone comes across this with a better solution, please share.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top