解决setBackground与在xml设置android:background不一致的问题
2013-04-22 12:18
295 查看
若在xml中有其他的layout 属性,会导致在代码中setBackground与android:background不一致的现象:
例如:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="12dp"
android:paddingBottom="4dp"
android:id="@+id/background"
android:background="@drawable/card_article_top"
>
若在代码中设置背景,则忽略了padding的效果,导致内容"放大"了,解决办法为:
public void setBackGround(int resourceId) {
int bottom = mBackground.getPaddingBottom();
int top = mBackground.getPaddingTop();
int right = mBackground.getPaddingRight();
int left = mBackground.getPaddingLeft();
mBackground.setBackgroundResource(resourceId);
mBackground.setPadding(left, top, right, bottom);
}
需要注意的是,需要先setBackground后,setPadding,否则无效~
其他属性类似~
例如:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="12dp"
android:paddingBottom="4dp"
android:id="@+id/background"
android:background="@drawable/card_article_top"
>
若在代码中设置背景,则忽略了padding的效果,导致内容"放大"了,解决办法为:
public void setBackGround(int resourceId) {
int bottom = mBackground.getPaddingBottom();
int top = mBackground.getPaddingTop();
int right = mBackground.getPaddingRight();
int left = mBackground.getPaddingLeft();
mBackground.setBackgroundResource(resourceId);
mBackground.setPadding(left, top, right, bottom);
}
需要注意的是,需要先setBackground后,setPadding,否则无效~
其他属性类似~
相关文章推荐
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- android软键盘把布局顶上去,在AndroidManiFest.xml设置windowSoftInputMode没有效果的问题解决
- 如何解决通过Eclipse建立AVD时保存路径和我通过设置环境变量ANDROID_SDK_HOME的路径不一致的问题?
- Android TextView中图文混排设置行间距导致高度不一致问题解决
- Android中EditView TextView (padding失效)使用setBackgroundDrawable或setBackgroundResource则xml中设置的 Padding失效
- 解决Android当中GridView的Item子控件中有ImageView图片大小不一致问题,动态设置ImageView控件大小
- 关于网上解决Android4.x系统设置字体大小导致应用布局混乱引起的问题
- 解决Activity启动黑屏及设置android:windowIsTranslucent不兼容activity切换动画问题
- Android闹钟启动时间设置无效问题的解决方法
- 【完美解决系列】解决在Android中使用background时导致图片被拉伸的问题
- android中setBackgroundColor()中不能设置背景颜色的问题
- 解决 Button 设置 background 之后点击动画效果消失的问题
- Android TabHost设置setCurrentTab(index),当index!=0时,默认加载第一个tab问题解决方法。
- Android解决不同分辨率距离一致的问题
- android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决
- Android设置软键盘搜索键以及监听搜索键点击时发生两次事件的问题解决
- 解决android:background背景图片被拉伸问题
- 解决Activity启动黑屏及设置android:windowIsTranslucent不兼容activity切换动画问题
- Android GridView之添加分隔线,动态设置高度,实现高度自适应,并解决第一个item不显示的问题
- Android 将Activity打成jar包 供第三方使用 彻底解决XML 图片 文字资源不可打入到jar包的问题!