关于Android的Lint检查的一些结果分析
2018-01-25 10:29
423 查看
前言
在安卓开发中,Lint可以用来帮助我们检查代码中存在的一些问题,使用方法也很简单,Analyze---Inspect CodeLint检查完成后,可以在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后面没加描述
相关文章推荐
- 关于Android Lint的一些警告处理
- Android面试系列文章2018之Andrlid代码分析lint检查篇
- 修改设置->关于手机->法律信息 下有一些Item,如开放源代码许可、Google法律信息等,这里分析的是Android 4.4的代码
- Android Lint 检查案例分析
- 关于Android主线程(ActivityThread)源代码分析以及一些特殊问题的非常规方法
- 关于Android屏幕适应的一些分析
- 关于侯垒的自增字段和GUID字段性能对比文章的一些自己的分析(没有测试,纯粹分析)
- 关于Android 开发的一些资料
- 【转】关于eclipse和javac编译结果不一致的问题的分析与解决
- common.inc.php--关于系统中$_REQUEST['GLOBALS']的一些分析
- 一些关于帮助初学者分析dump的链接
- 关于数值分析和LCP问题的一些开源项目
- 关于android开发遇到的一些问题的解决办法---间断的更新
- 关于Android中传递数据的一些讨论
- 俺网站Android及Ophone开发,全系列上手资料,关于环境搭建遇见的一些问题,包1天能上手。
- 关于Android中传递数据的一些讨论
- 关于Android中传递数据的一些讨论 intent行不通,static不让用的时候用application
- 关于Android的一些体验
- 关于抽象代数(16、17)中一些零散概念间联系的个人分析总结
- 关于递归的一些分析