日常记录-代码中Background后Padding 失效

来源:互联网 发布:建筑工程基础知识软件 编辑:程序博客网 时间:2024/06/03 21:05

  近日,在开发过程中 遇到了 Layout 代码中设置 Background 后,padding失效的问题,只是在Android 4.4.4 和 4.4.2 的手机上遇到了。

网上搜索了下,说是 4.4 系统里的一个bug,解决方法就是 在动态设置 Background 后,重新设置 padding。

  解决方法如下:

 

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

 

原帖网址

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

阅读全文
0 0
原创粉丝点击