Preface
After the background and padding are specified in Layout, the dynamic modification of the background in the program will render the padding invalid. It seems to be a BUG. Here I found an English article to share it with you.
StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com
Body
I. Compromise
1.1 method 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 );
1.2 method 2
Int pad = resources. getDimensionPixelSize (R. dimen. linear_layout_padding );
TheView. setBackgroundResource (R. drawable. entry_bg_with_image );
TheView. setPadding (pad, pad );
Code Description:
In fact, it is to reset the padding after setBackgroundResource.
Ii. Original Post URL
Http://stackoverflow.com/questions/5890379/android-setbackgroundresource-discards-my-xml-layout-attributes
3. Search for keywords
GOOGLE: "android setbackgroundresource changes padding"
End
Search engines can basically solve 99% of my problems. Chinese keywords cannot be changed to English keywords, or you can directly search for code. Beginners should regard search as their most important learning tool as soon as possible, I will try my best to add search keywords to similar articles in the future.