前言

在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"

4 找到了官方的注释:

* Set the background to a given Drawable, or remove the background. If the
* background has padding, this View's padding is set to the background's
* padding. However, when a background is removed, this View's padding isn't
* touched. If setting the padding is desired, please use
* {@link #setPadding(int, int, int, int)}.

就是说如果设置了backGround, padding就换置为background上面的padding了。 所以要重新设置。

[Android疑难杂症]动态改变Background后Padding无效的问题的更多相关文章

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

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

  2. Android中动态改变控件的大小的一种方法

    在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...

  3. Android 如何动态改变Actionbar上的item图标

    1.Activity菜单机制 (与dialog类似) Activity有一套机制来实现对菜单的管理,方法如下: 1.public boolean onCreateOptionsMenu(Menu me ...

  4. Android疑难杂症之KillProcess 和System.exit 无效

    以下所讲,浓缩在 https://github.com/wytings/CrashDemo 首先就这个名字来说,kill了process 或者 system.exit确实已经把APP杀掉了,特别是当你 ...

  5. Android实现动态改变屏幕方向(Landscape & Portrait)

    1.AndroidManifest.xml: <activity> android:screenOrientation="portrait" ... 2.xx.java ...

  6. Android如果动态改变CursorAdapter Item个数

    //adapter内部类 private class SearchAdapter extends CursorAdapter { @Override public View newView(Conte ...

  7. Android中动态改变Listview中字体的颜色

    效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...

  8. Android ListView动态改变Item高度

    在adapter的getView方法中进行设置,代码如下 @Override public View getView(int position, View convertView, ViewGroup ...

  9. Android之动态改变控件大小

    利用getLayoutParams()方法和setLayoutParams()方法.三步曲:1.首先利用getLayoutParams()方法,获取控件的LayoutParams.eg:LayoutP ...

随机推荐

  1. generator函数

    function* helloWordGenerator() { yield "hello"; yield "world"; return "endi ...

  2. eclipse luna maven搭建spring mvc

    1. 环境配置 a)         Java 1.7 b)         Eclipse luna c)         Maven3.2.5 d)         Spring 4.1.4 2. ...

  3. Httpservlet cannot be resolved to a type的原因与解决方法

    刚开始学习Servlet,在Eclipse中新建了一个Servlet,不过页面上报错: Httpservlet cannot be resolved to a type,显然是Eclipse找不到相应 ...

  4. Hadoop常见错误解决

    1. 通过命令和查看日志文件查看hadoop启动和运行情况 在NameNode端,可以通过 tail -100 /var/log/hadoop/hadoop/hadoop-hadoop-namenod ...

  5. [转] Neutron FWaaS

    OpenStack Neutron FWaaS 学习 ( by quqi99 ) 作者:张华  发表于:2013-06-24 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息 ...

  6. 加了GO后报 'GO' 附近有语法错误

    单独运行SQL无问题,了加GO就报错. 是你的SQL语句中,有些行的结尾处只有Char(13)没有Char(10),即:只有回车符没有换行符,这种状态在视觉上是没办法区分的. 参考:http://ww ...

  7. 【mybatis】之批量添加

    mybatis批量添加xml <insert id="batchCreate"> INSERT INTO `roomer` (`order`,name,idCard,m ...

  8. purgeIdleCellConnections: found one to purge conn = 0x1e09f7d0

    purgeIdleCellConnections: found one to purge conn = 0x1e09f7d0 你在iOS6下使用3G网络时可能会遇到这条log,不用紧张,这只是苹果的工 ...

  9. 黄聪:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法(转)

    转自:http://www.cnblogs.com/susuyu/archive/2013/05/28/3104249.html 环境:linux,mysql5.5.21 错误:Host is blo ...

  10. 黄聪:Discuz!X3.2 如何配置超级版主或者某些管理员,允许管理用户组或者权限

    点击后台-->站长-->后台管理团队-->新增用户(用户名)用户管理员即可