Android 更新背景边界

Android 更新背景边界,android,background,android-custom-view,Android,Background,Android Custom View,我正在尝试更新EditText视图的背景边界,以便最终结果类似于以下内容 +----------------+ | Empty Space | | | | +------------+ | | | Background | | | +------------+ | +----------------+ 我目前的方法是在onLayout中获取背景,然后简单地更新边界 @Override protected void onLayout(boolean chang

我正在尝试更新EditText视图的背景边界,以便最终结果类似于以下内容

+----------------+
|  Empty Space   |
|                |
| +------------+ |
| | Background | |
| +------------+ |
+----------------+
我目前的方法是在
onLayout
中获取背景,然后简单地更新边界

@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);
}

然而,这根本不起作用。正在应用边界,但当它绘制时,它不会更改。最近的一次,是在
onDraw
中更改边界,然而,它最初将在其原始位置绘制,然后立即重新绘制到其新位置。。。如何可靠地更改背景边界?

经过进一步的研究,我能够解决这个问题的唯一方法是创建一个中间可绘制对象(中间人),并将所有公共方法委托给实际可绘制对象。然后覆盖
setBounds
以设置我想要的任何值

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);
    }

    ...
  }
}

很黑。如果有人遇到了更好的解决方案,请分享。

另一种方法是覆盖
视图
类中的
draw(Canvas Canvas)
方法,并在绘制背景之前设置边界,在我看来,背景也很混乱。你的方法可能是最好的方法。