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

前言

在Layout中指定好background和padding以後,程序裏面動態修改background之後padding就失效了,貌似是一個BUG,這裏找到了一篇英文文章,簡單翻譯分享一下。



 

正文

一、折中辦法

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


轉自:http://www.cnblogs.com/over140/archive/2012/07/20/2600525.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章