关于Android的Lint检查的一些结果分析

前言

在安卓开发中,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后面没加描述













  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值