前言
在安卓开发中,Lint可以用来帮助我们检查代码中存在的一些问题,使用方法也很简单,Analyze---Inspect Code
Lint检查完成后,可以在Inspection Results中看到检查结果
下面总结一些常见的lint问题
1.Image without contentDescription,没有给ImageView指定contentDescription属性,该属性的作用是主要用于有障碍的人士使用
正确写法如下,如果你想忽略警告
<ImageView
android:id="@+id/iv_top_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/address"
android:contentDescription="@null"
/>
2.Keyboard inaccessiable widget,给控件添加了clickable属性,但是没添加focusable,一个控件,如果没有定义focusable(可聚焦的),却定义了是clickable(可点击的),
那么是不能通过键盘访问的。所以,需要添加一个focusable="true"。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
/>
3.Implied locale in date format
传统写法(根据指定的格式创建SimpleDateFormat对象,会有警告)
dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT);
dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT);
dateTimeFormat2 = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT_2);
修正之后的
dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT,Locale.getDefault);
dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT,Locale.getDefault);
dateTimeFormat2 = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT_2,Locale.getDefault);
4.LayoutInflation without a parent,如果我们按照下面的方法填充布局,就会有一个警告
View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null, false);
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element
为什么会有这个警告呢,因为如果我们传null的话,我们给根布局设置的宽高就会失效
5.Missing commit on Sharepreference editor,获取editor后没有提交。
6.Overlapping items in RelativeLayout:RelativeLayout中的条目可能重叠,
7.Padding and margin symmetry:意思是说左右的padding都要指定,为了对称
8.Using left/right instead of start/end attribute,一般是让我们添加layout_toEndOf,marginStart,drawableStart,
alignParent_End等方法,以便支持从右向左的布局
9.Memory allocations withing drawing code:一般是在onDraw里面创建对象
10.OverDraw:Painting regions more than once
Possible overdraw: Root element paints background @drawable/lavagna_verticale with a
theme that also paints a background (inferred theme is @android:style/Theme.Holo)
11.while loop replaceable with foreach:有这个警告是因为在遍历集合的时候使用了while,因为集合的长度一般是有限的
所以while此时不适用,
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
headerBuilder.add((String) entry.getKey(), (String) entry.getValue());
}
12.Explicit Type can be replace with <>
ArrayList<String> list = new ArrayList<String>();
正确写法是去掉后面泛型的类型,就没有警告了
13.Declaration has JavaDoc Problems:
/**
* 移除某个key值已经对应的值
*
* @param key
*/
public static void remove(String key) {
editor.remove(key);
editor.commit();
}
这样写有警告,@param key tag description missing,意思是说key后面没加描述