前言
在Layout中指定好background和padding以后,程序里面動態修改background之后padding就失效了,貌似是一個BUG,這里找到了一篇英文文章,簡單翻譯分享一下。
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
正文
一、折中辦法
1.1 方法一
int bottom = theView.getPaddingBottom();
int top = theView.getPaddingTop();
int right = theView.getPaddingRight();
int left = theView.getPaddingLeft();
theView.setBackgroundResource(R.drawable.entry_bg_with_image);
theView.setPadding(left, top, right, bottom);
int top = theView.getPaddingTop();
int right = theView.getPaddingRight();
int left = theView.getPaddingLeft();
theView.setBackgroundResource(R.drawable.entry_bg_with_image);
theView.setPadding(left, top, right, bottom);
1.2 方法二
int pad = resources.getDimensionPixelSize(R.dimen.linear_layout_padding);
theView.setBackgroundResource(R.drawable.entry_bg_with_image);
theView.setPadding(pad, pad, pad, pad);
theView.setBackgroundResource(R.drawable.entry_bg_with_image);
theView.setPadding(pad, pad, pad, pad);
代碼說明:
實際上就是在setBackgroundResource之后重新設置一下padding。
二、原帖網址
三、搜索關鍵字
GOOGLE:"android setbackgroundresource changes padding"
結束
搜索引擎基本能解決我99%的問題,中文關鍵字不行換英文關鍵字,或者直接搜索代碼,初學者應把搜索盡快當成自己最重要的學習工具,以后類似的文章我也盡量把搜索關鍵字給加上。