今天在项目中用到了控件 ImageView 的 setImageLevel 方法,也遇到了很多坑
使用方法的话就是在 drawable 下创建一个 xxx.xml 文件,代码如下:
一、第一个坑
必须用src,不能用background,否则你会看到你的所有item都会显示0号下表的第一张图片,自己可以试试
或者这样设置也行
ImageView imageView = findViewById(R.id.xxx);
imageView.setImageResource(R.drawable.level-list);
最后在代码中改变图片的状态
// 变化的 state,你那边自己定义的
if (state == 0) {
imageView.setImageLevel(0);
} else if (state == 1) {
imageView.setImageLevel(1);
}
//......等,用 switch 写也行
二、第二个坑
如果我的xml是这样写的,也是会报错的,maxLevel 不能有负值
代码如下:
报错如下:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'maxLevel' attribute
Caused by: java.lang.reflect.InvocationTargetException
Error inflating class