先看网上的例子
try {
Field f = AbsListView.class.getDeclaredField(“mFastScroller”);}
在实际应用中,该代码会出现异常,通过对几个sdk源码的对比,发现google会对其中的属性做一些微调:
如在5.x中,“mFastScroller”改为了“mFastScroll”,4.4中则把“mThumbDrawable”改为“thumbDrawable”并设为了final,在5.x中又恢复成了private.
所以在实际应用中还需加以判断。下面是针对4.4修改后的代码:
由于class FastScroller没有public属性,无法直接导包获取到,所以从用到该类的AbsListView中获取。
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller"); //获取AbsListView中的属性mFastScroller
f.setAccessible(true);//设置属性可修改
Object o = f.get(listview);//得到listview实例
// Field[] fields = f.getType().getDeclaredFields();
// for (Field field : fields) {
// Log.v("TAG", field.getName());
// } //查看所有属性名
f = f.getType().getDeclaredField("mThumbImage");//获取属性mThumbImage(由于 4.4中的thumbDrawable不可修改,所以直接取其imageview)
f.setAccessible(true);
ImageView img = (ImageView) f.get(o); //得到ImageView实例
img.setImageDrawable(getResources().getDrawable(R.drawable.icon));
f.set(o, img); //把编辑好的ImageView放进去
} catch (Exception e) {
throw new RuntimeException(e);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011808801/article/details/47277509