前言

在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. js和css内联外联注意事项

    简单说:这两个问题其实是同一个问题,但是网上找了好久也找不到方法,外联的js和css文件里不能有任何HTML的标记注释,一旦有,浏览器就疯了!一去掉就好了!!! 问题:起因是网上看到一个css的表格样 ...

  2. Struts2 - 传值

    Struts2传值有几种方式: 1)URL parameter 例如配置代码: <action name="login_*" class="com.my.actio ...

  3. Window下Nexus私服搭建

    项目组大部分人员不能访问maven的central repository,因此在局域网里找一台有外网权限的机器,搭建nexus私服,然后开发人员连到这台私服上  环境是:nexus-2.1.1.mav ...

  4. docker的例子

    定制镜像 做个测试服务器,testServer代码如下 package main import ( "net/http" ) func main() { http.Handle(& ...

  5. 如何配置apache最大的并发数

    如何配置apache最大的并发数MPM(多路处理模块)常见:1.perfork 预处理进程方式2.worker 工作者模式3.winnt 在windows使用 案例:把apache的最大并发数配置成1 ...

  6. Android sqlite管理数据库基本用法

    Android操作系统中内置了sqlite数据库(有关sqlite数据库详细介绍见:http://zh.wikipedia.org/wiki/SQLite),而sqllite本身是一个很小型的数据库, ...

  7. Report_报表中Ref Cursor数据源的概念和用法(案例)

    2014-06-22 Created By BaoXinjian

  8. OAF_OAF Debug And Log调试和记录工具的详解(案例)

    2014-06-16 Created By BaoXinjian

  9. NeHe OpenGL教程 第十九课:粒子系统

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. linux 常用alias

    alias qqcom='cd /usr/local/qqcom_app/' alias php_c='cd /usr/local/php/lib/' alias ap_c='cd /usr/loca ...