Where'd padding go, when setting background Drawable?

后端 未结 16 1359
攒了一身酷
攒了一身酷 2020-12-03 00:41

I have this issue on my EditText and Button views, where I have a nice padding for them to space away from the text, but when I change the backgrou

16条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 00:57

    I had this problem in a TextView, so I subclassed TextView and made an Override method of the TextView.setBackgroundResource(int resid) method. Like this:

    @Override
    public void setBackgroundResource(int resid) {
        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();
    
        super.setBackgroundResource(resid);
    
        this.setPadding(pl, pt, pr, pb);
    }
    

    This way, it gets the padding of the item before it sets the resource, but doesn't actually mess with the original functionality of the method, other than keeping the padding.

提交回复
热议问题