[Android疑難雜症]動態改變Background后Padding無效的問題



前言

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

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

代碼說明:

實際上就是在setBackgroundResource之后重新設置一下padding。 

 

二、原帖網址

http://stackoverflow.com/questions/5890379/android-setbackgroundresource-discards-my-xml-layout-attributes

 

三、搜索關鍵字

GOOGLE:"android setbackgroundresource changes padding

 

結束 

搜索引擎基本能解決我99%的問題,中文關鍵字不行換英文關鍵字,或者直接搜索代碼,初學者應把搜索盡快當成自己最重要的學習工具,以后類似的文章我也盡量把搜索關鍵字給加上。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM