设置背景Drawable时,填充会去哪里? [英] Where'd padding go, when setting background Drawable?

查看:60
本文介绍了设置背景Drawable时,填充会去哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EditTextButton视图上遇到了这个问题,在这里我有很好的填充来使它们与文本间隔开,但是当我用setBackgroundDrawablesetBackgroundResource更改背景时,填充是永远失去了.

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 background with setBackgroundDrawable or setBackgroundResource that padding is lost forever.

推荐答案

我发现添加9色块作为背景资源重置了填充-尽管有趣的是,如果我添加了彩色或非9色块图像,则没有没错解决方案是在添加背景之前保存填充值,然后再设置它们.

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

这篇关于设置背景Drawable时,填充会去哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆