[Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
2011-08-28 21:46
1106 查看
因为需求的缘故,需要对liview显示项做黑白相间的处理:
其实就是在函数public View getView(int position, View convertView, ViewGroup parent)
中,加上:
这可怎么办呢?我在xml中直接设置9-patch给一个layout什么的,都是正常显示呀,这个怎么就出问题了呢?
问题在于以下函数:
public void setBackgroundResource (int resid) Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
也就是说,其实setBackgroundResource接着调用的应该是:setBackgroundDrawable函数,让我们看看setBackgroundDrawable的描述:
public void setBackgroundDrawable (Drawable d) Since: API Level 1 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 setPadding(int,
int, int, int).Parametersd The Drawable to use as the background, or null to remove the background
也就是说,因为9-patch有自己的padding,所以convertView自己的padding被覆盖了!那可咋办呢?
其实知道这个了就很简单了,只要在代码setBackgroundResource之后加上(Remember:一定是这句之后!)一句:
convertView.setPadding(0, 0, 0, 0);
就可以解决问题了~
是不是挺奇怪的,其实很多时候我们有些问题解决不了是因为不了解其内部机制(当然有些时候其内部机制稍显怪诞),只要了解了,便可以应用自如了。
希望各位能从我的文章中找到有用的东西~~~
其实就是在函数public View getView(int position, View convertView, ViewGroup parent)
中,加上:
if (position % 2 == 0) { convertView.setBackgroundResource(R.drawable.list_gray_9); } else { convertView.setBackgroundResource(R.drawable.list_white_9); }可是,正如上述代码可见,我加入的是9-patch图片,直接导致我的convertView中的内容无法正常显示了。
这可怎么办呢?我在xml中直接设置9-patch给一个layout什么的,都是正常显示呀,这个怎么就出问题了呢?
问题在于以下函数:
public void setBackgroundResource (int resid) Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
也就是说,其实setBackgroundResource接着调用的应该是:setBackgroundDrawable函数,让我们看看setBackgroundDrawable的描述:
public void setBackgroundDrawable (Drawable d) Since: API Level 1 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 setPadding(int,
int, int, int).Parametersd The Drawable to use as the background, or null to remove the background
也就是说,因为9-patch有自己的padding,所以convertView自己的padding被覆盖了!那可咋办呢?
其实知道这个了就很简单了,只要在代码setBackgroundResource之后加上(Remember:一定是这句之后!)一句:
convertView.setPadding(0, 0, 0, 0);
就可以解决问题了~
是不是挺奇怪的,其实很多时候我们有些问题解决不了是因为不了解其内部机制(当然有些时候其内部机制稍显怪诞),只要了解了,便可以应用自如了。
希望各位能从我的文章中找到有用的东西~~~
相关文章推荐
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- Android代码code设置9.png/9-patch 图片背景后,此view中的控件显示不正常
- Android开发全程记录(八)——设置ImageView显示的图片铺满全屏(适应魅族等不常见屏幕比例)
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- Android 代码中设置控件背景颜色,以TextView为例
- Android开发技巧——TextView加载HTML的图片及代码显示问题
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- Android开发技巧——TextView加载HTML的图片及代码显示问题
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android textview在code(代码)中设置图片 和XML设置图片
- Android TextView 显示HTML代码以及代码中的图片标签
- 最新历史版本 :android--设置TextView部分文字的颜色和背景(高亮显示)
- 1.Android开发---从简单的TextView看Android的控件显示方式