因为需求的缘故,需要对liview显示项做黑白相间的处理:
其实就是在函数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);
就可以解决问题了~
是不是挺奇怪的,其实很多时候我们有些问题解决不了是因为不了解其内部机制(当然有些时候其内部机制稍显怪诞),只要了解了,便可以应用自如了。
希望各位能从我的文章中找到有用的东西~~~
分享到:
相关推荐
Sharp is a Scalable Vector Graphics (SVG) implementation for Android. It facilitates loading vector graphics as SharpDrawables, and can effectively be used wherever a conventional image would be ...
一、[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android.text.Html; TextView t3 = (TextView) findViewById(R.id.text3); t3.setText( ...
[Android开发从零开始].4.TextView控件学习
ExpandableTextView library is a similar copy of ExpandableTextView by Manabu Shimobe , but change a little UI structure and improve the code to make it easier to use. Setup Download the library ...
FastTextView FastTextView is faster than Android TextView. FastTextView use StaticLayout to render Spanned String, so it support most features of Android TextView. Inspired by : ...
Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商...
设置view的圆角和背景一般都是在xml里面通过selector的配置文件来...在项目中有个需求,背景颜色是从网络获取的,而且要设置为圆角,想了一下,通过xml的形式貌似实现不了,只能通过代码分别设置,就只能重写Textview。
android下textview 显示html片段,可以显示文字和图片混排,图片可以是网络图片
android控件详解----TextView
这是参考别人的显示gif的textview而写出来的,可同时显示gif和png还有文字,类似于QQ聊天时输入动态表情, 不足的是,由于各gif的频率不一致,在输入多个不同gif时,有些gif可能看起来会慢一些 参考:...
Android的TextView中显示图片和图像的方法
android 文字图片混排,在一个TextView中显示文字穿插图片,文字变色效果.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
在android 7.0上工作不好,不推荐。
Android View initializer and Click Listener boiler plate code remover This library is based on the ideas of removing boilerplate code like ButterKnife but with use cases targeted for view binding of ...
FlexibleRichTextView一个可以显示LaTeX、图片、代码的TextView,你可以自定义标签。
在线安装: Eclipse 3.5 (Galileo) and 3.6 (Helios) https://dl-ssl.google.com/android/eclipse/ Eclipse 3.4 (Ganymede) https://dl-ssl.google.com/android/eclipse
解决Android中原生TextView显示中英文特殊字符等末尾参差不齐问题
在TextView中添加图片的三种方式总结 1.使用SpannableString+ImageSpan 2.android:drawableBottom... 3。扩展TextView 复写onDraw
Android-ShoppingCartAddSubtract 一种漂亮的UI控件,能更灵活的控制数字的增减。 项目地址:https://github.com/open-android/ShoppingCartAddSubtract 简书:http://www.jianshu.com/p/be3d0f763c5a 运行效果 爱...
button" ...//设置 button 样式 android:textStyle="bold"/> <Button android:id="@+id/about_button" ...//设置 button 样式 android:textStyle="bold"/> </LinearLayout> </RelativeLayout> figure-10 ...