Android Lint contentDescription警告

136

在使用Android Lint时,我收到了一个警告:“[Accessibility] Missing contentDescription attribute on image”,指的是ImageView缺少contentDescription属性。

这是什么意思?


5
这个警告太烦人了,特别是对于那些仅仅用于装饰的图片。 - Someone Somewhere
6
我在strings.xml文件中定义了这个:<string name="none"></string>,然后我使用了android:contentDescription="@string/none" - Someone Somewhere
11个回答

176

通过为我的ImageView设置属性android:contentDescription,解决了这个警告。

android:contentDescription="@string/desc"

在ADT 16中的Android Lint支持会发出此警告,以确保图像小部件提供contentDescription。

这定义了简要描述视图内容的文本。此属性主要用于可访问性。由于一些视图没有文本表示,因此可以使用此属性来提供文本描述。

非文本小部件(例如ImageViews和 ImageButtons)应使用contentDescription属性指定小部件的文本描述,使屏幕阅读器和其他辅助功能工具能够充分描述用户界面。


4
您可以通过访问以下链接了解更多信息并进行测试:http://android-developers.blogspot.com/2012/04/accessibility-are-you-serving-all-your.html 和 http://developer.android.com/guide/topics/ui/accessibility/apps.html#test - android developer

52

禁用Lint警告很容易让你以后陷入麻烦。 最好为所有的ImageView指定contentDescription。 如果您不需要描述,请使用:


禁用Lint警告会给将来带来麻烦,最好为所有的ImageView指定contentDescription。如果不需要描述,则可以使用以上提到的方式。
android:contentDescription="@null"

41

另一个选项是逐个禁止警告:

xmlns:tools="http://schemas.android.com/tools"  (usually inserted automatically)
tools:ignore="contentDescription"

示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    tools:ignore="contentDescription" >

       <ImageView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:padding="5dp"
            android:src="@drawable/icon" />

错误 - 在RelativeLayout中添加tools:ignore =“contentDescription”导致编译错误“属性缺少Android命名空间前缀”。 - G. Kh.
3
这是一个关于 Eclipse 的问题。只需要清理一下你的项目。同时确保:xmlns:tools="http://schemas.android.com/tools" 也被包含在内! - Gunnar Bernstein

25

我建议您添加 contentDescription。

android:contentDescription="@string/contentDescriptionXxxx"

但是,让我们现实一些。大多数人不会为了无障碍性而保留原意。 不过,只需付出一点努力,您就可以实施一些帮助残障人士的措施。

<string name="contentDescriptionUseless">deco</string>
<string name="contentDescriptionAction">button de action</string>
<string name="contentDescriptionContent">image with data</string>
<string name="contentDescriptionUserContent">image from an other user</string>

盲人用户最需要知道的是"我需要点击哪个按钮才能继续"

对于任何可点击的内容,请使用contentDescriptionAction。

对于带有信息的图像(图表,文本作为图像等),请使用contentDescriptionContent。

对于所有由用户提供的内容,请使用contentDescriptionUserContent。

对于其他所有无用的内容,请使用contentDescriptionUseless。


1
谢谢!提供这个描述总是比抑制警告更好的选择。 - Vinicius Paldês

12

由于这只是一个警告,您可以忽略它。请进入XML的图形化布局并执行以下操作:

  1. 点击右上角的红色按钮

    enter image description here

  2. 选择“禁用问题类型”(例如)

    enter image description here


5
是的,你确实可以抑制它,但出于依赖Android提供的辅助工具的用户的考虑,你可能应该遵循所选答案的建议。 - Kyle Falconer
就是这个了!!!这就是我一直在寻找的。这个答案和@Gunnar Bernstein的答案帮了我很大的忙。 - IronBlossom

8

打开Gradle文件(模块应用程序),添加以下代码块:

android {
    ... 
    lintOptions {
        disable 'ContentDescription'
    }
    ...
}

不再有警告!愉快编码


愉快的编码,但请注意,这将损害实际依赖它的人的可访问性。 - I'm a frog dragon

7
如果您希望以优雅的方式抑制此警告(因为您确定该特定 ImageView 不需要可访问性),则可以使用特殊属性:
android:importantForAccessibility="no"

2

对于纯粹装饰用途的图形元素,请将它们相应的android:contentDescription XML属性设置为 "@null"。

如果您的应用程序仅支持运行Android 4.1(API级别16)或更高版本的设备,则可以将这些元素的android:importantForAccessibility XML属性设置为“no”。


android:importantForAccessibilty 正是我一直在寻找的。谢谢! - Hrafn
这确实是正确的答案,应该是最佳答案。 - lsrom

2

Android可访问性需要ContentDescription,特别是针对屏幕阅读器功能。

如果您不支持Android可访问性,则可以通过设置Lint来忽略它。

因此,只需创建lint.xml即可。

<?xml version="1.0" encoding="UTF-8"?>
<lint>

    <issue id="ContentDescription" severity="ignore" />

</lint>

将其放置在app文件夹中。

输入图像描述


1
非文本小部件需要一些方式的内容描述,以文本方式描述图像,以便屏幕阅读器能够描述用户界面。您可以忽略属性xmlns:tools="http://schemas.android.com/tools"
tools:ignore="contentDescription"
或定义属性android:contentDescription="your description"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接