lint 相关问题总结

本文详细解析了Android Studio中Lint工具的各种警告信息及其解决方案,包括标签类和代码类的问题,并提供了具体的修正方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载至简书:https://www.jianshu.com/p/bb712fc4ab1c

Android Studio 中使用 Lint
工具栏 -> Analyze -> Inspect Code…

至于lint是什么……请自行百度搜索……
这是总结以往lint是怎么解决黄色警告的,以及原因。
同时分几大类。

1.标签类:

1.1.Element arcMotion is not allowed here

指的是arcMotion该标签不允许。只要在标签前面添加suppress AndroidElementNotAllowed即可

 

image.png

1.2.Missing 'contentDescription' attribute on image

需要给图片添加备注。
一般来说我选择忽略,添加tools:ignore="ContentDescription"

1.3.'clickable' attribute found, please also add 'focusable'

一个控件(比如图片),如果没有定义focusable(可聚焦的),却定义了是clickable(可点击的),那么是不能通过键盘访问的。所以,需要添加一个focusable="true"。

1.4.Missing accessibility label: where minSdk < 17, you should provide an 'android:hint'

意思是要添加hint,如果不想添加hint,那么就选择忽略,添加tools:ignore="LabelFor"

1.5."'xxxx'" is not translated in "zh" (Chinese)

如果一个字符串不需要翻译,可以添加属性translatable="false"在<string>标签里。如:

<string name="name" translatable="false">Android</string>
或者把你不需要翻译的字符串统一放到一个
donottranslate.xml
中去。或者给根标签加一个属性tools:ignore="MissingTranslation"来忽略这个问题。

1.6.'A' can overlap 'B' if 'C' grows due to localized text expansion

原因:此提示一般出现在RelativeLayout中,此提示的大体意思为:你某个布局(一般是TextView,因为TextView的长度会随着文本内容扩大而扩大)可能会重叠另外一个布局。
如果还不理解,可以给该textview的文本写上超长内容就能看到问题了~
解决方式如下:
一:确保该文本长度不会覆盖的话,选择忽略。tools:ignore="RelativeOverlap"
二:使用RelativeLayout的布局属性,设置该textview的长度不会覆盖

1.7.Set 'android:baselineAligned="false"' on this element for better performance

原因是:当LinerLayout的子View都是ViewGroup(自定义控件除外)时,Lint认为它的子View已经不需要基准线对齐了,为了不让LinerLayout去做无用的计算对齐的操作,提出了如上警告,修改掉之后就可以提高性能。

1.8.Nested weights are bad for performance

原因是:嵌套权重对性能不好
解決方式:使用百分比布局或者约束布局ConstraintLayout或者其他方式,反正就是避免嵌套权重

2.代码类:

2.1.Custom view 'XXXXX' has 'setOnTouchListener' called on it but does not override 'performClick'

这个貌似是从某个版本更新后出现的一个新警告,这个警告是告诉你点击事件会跟OnTocuch事件冲突。
基本上这意味着你应该继承该view的子类重写其performclick()方法,但这是不必要的。在确保有执行OnTouchlistener的情况下,我们只需要抑制这种警告使用@SuppressLint("ClickableViewAccessibility")就可以了。

2.2.This custom view should extend 'android.support.v7.widget.AppCompatImageView' instead

extends ImageView 改成 extends android.support.v7.widget.AppCompat
原因:Using AppCompat widgets allows you to have some material design (and other new) features on devices with pre-Lollipop versions of Android.

At this point AppCompatImageView only provides the support for background tint and vector drawables. If you don't use them, then extending the regular ImageView will be fine.

2.3.Switch statement on an 'int' with known associated constant missing case 'XXXX', 'XXX'

指明该swith少了'XXXX',要么忽略,要么加上。



作者:qq名长是因为你没给我备注
链接:https://www.jianshu.com/p/bb712fc4ab1c
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值