[Difficult for Android] The problem of dynamic change of the Padding after the Background is invalid

Source: Internet
Author: User

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.


Statement
Welcome 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.