Where'd padding go, when setting background Drawable?

后端 未结 16 1265
攒了一身酷
攒了一身酷 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:56

    My solution was to extend the view (in my case an EditText) and override setBackgroundDrawable() and setBackgroundResource() methods:

    // Stores padding to avoid padding removed on background change issue
    public void storePadding(){
        mPaddingLeft = getPaddingLeft();
        mPaddingBottom = getPaddingTop();
        mPaddingRight = getPaddingRight();
        mPaddingTop = getPaddingBottom();
    }
    
    // Restores padding to avoid padding removed on background change issue
    private void restorePadding() {
        this.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
    }
    
    @Override
    public void setBackgroundResource(@DrawableRes int resId) {
        storePadding();
        super.setBackgroundResource(resId);
        restorePadding();
    }
    
    @Override
    public void setBackgroundDrawable(Drawable background) {
        storePadding();
        super.setBackgroundDrawable(background);
        restorePadding();
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 00:57

    In my case I had a drawable and was so stupid I didn't see the paddings were all set to 0 in the xml.

    0 讨论(0)
  • 2020-12-03 00:58

    just change lib to v7:22.1.0 in android studio like this compile 'com.android.support:appcompat-v7:22.1.0'

    0 讨论(0)
  • 2020-12-03 00:59

    What I found was adding a 9 patch as a background resource reset the padding - although interestingly if I added a color, or non-9 patch image, it didn't. The solution was to save the padding values before the background gets added, then set them again afterwards.

    private EditText value = (EditText) findViewById(R.id.value);
    
    int pL = value.getPaddingLeft();
    int pT = value.getPaddingTop();
    int pR = value.getPaddingRight();
    int pB = value.getPaddingBottom();
    
    value.setBackgroundResource(R.drawable.bkg);
    value.setPadding(pL, pT, pR, pB);
    
    0 讨论(0)
  • 2020-12-03 00:59

    Combining the solutions of all, I wrote one in Kotlin:

    fun View.setViewBackgroundWithoutResettingPadding(@DrawableRes backgroundResId: Int) {
        val paddingBottom = this.paddingBottom
        val paddingStart = ViewCompat.getPaddingStart(this)
        val paddingEnd = ViewCompat.getPaddingEnd(this)
        val paddingTop = this.paddingTop
        setBackgroundResource(backgroundResId)
        ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
    }
    
    fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
        val paddingBottom = this.paddingBottom
        val paddingStart = ViewCompat.getPaddingStart(this)
        val paddingEnd = ViewCompat.getPaddingEnd(this)
        val paddingTop = this.paddingTop
        ViewCompat.setBackground(this, background)
        ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
    }
    
    0 讨论(0)
提交回复
热议问题