我得到一个关于[可访问性]在eclipse图像上缺少contentDescription属性的警告。这个警告显示在下面XML代码中的第5行(声明ImageView)。

这不会使任何错误时,构建和运行我的应用程序。但我真的想知道为什么我得到这个警告。

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/contact_entry_image"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/contact_entry_text"
        android:text=""
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

</LinearLayout>

请帮助我,感谢您的阅读。


当前回答

警告确实很烦人,在许多(大多数!)情况下,对于各种装饰性的ImageViews,没有必要使用contentDescription。解决这个问题最根本的方法就是告诉Lint忽略这个检查。在Eclipse中,转到Preferences中的“Android/Lint Error Checking”,找到“contentDescription”(它在“Accessibility”组中),并将“Severity:”更改为“Ignore”。

其他回答

此警告试图提高应用程序的可访问性。

要在整个项目中禁用缺失内容描述警告,可以将其添加到应用程序模块build.gradle中

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}

它给你警告,因为图像描述没有定义。

我们可以通过在Strings.xml和activity_main.xml中添加下面的代码来解决这个警告

在Strings.xml中添加下面这行

<string name="imgDescription">Background Picture</string>
you image will be like that:
<ImageView
        android:id="@+id/imageView2"
        android:lay`enter code hereout_width="0dp"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imgDescription"
        app:layout_editor_absoluteX="0dp"
        app:layout_editor_absoluteY="0dp"
        app:srcCompat="@drawable/background1"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

还要在activity_main.xml中添加这一行

android:contentDescription="@string/imgDescription"

Strings.xml

<resources>
    <string name="app_name">Saini_Browser</string>
    <string name="SainiBrowser">textView2</string>
    <string name="imgDescription">BackGround Picture</string>
</resources>

接下来,对于纯装饰性的图形元素,最好的解决方案是使用:

android:importantForAccessibility="no"

如果您的最小SDK版本至少是16,这是有意义的,因为运行较低版本的设备将忽略此属性。

如果你坚持支持旧版本,你应该使用(就像其他人已经指出的那样):

android:contentDescription="@null"

来源:https://developer.android.com/guide/topics/ui/accessibility/apps #标签元素

更新:

正如评论中指出的那样,将描述设置为null表示该图像纯粹是装饰性的,并且被TalkBack等屏幕阅读器理解为。

老答案,我不再支持这个答案:

对于所有想要避免警告的人:

我不认为android:contentDescription="@null"是最好的解决方案。

我正在使用工具:ignore="ContentDescription",这就是应该是什么。

确保在根布局中包含xmlns:tools="http://schemas.android.com/tools"。

按照这个链接的解决方案:Android Lint内容描述警告

Resolved this warning by setting attribute android:contentDescription for my ImageView android:contentDescription="@string/desc" Android Lint support in ADT 16 throws this warning to ensure that image widgets provide a contentDescription This defines text that briefly describes the content of the view. This property is used primarily for accessibility. Since some views do not have textual representation this attribute can be used for providing such. Non-textual widgets like ImageViews and ImageButtons should use the contentDescription attribute to specify a textual description of the widget such that screen readers and other accessibility tools can adequately describe the user interface.

这个链接的解释是:易用性,它的影响和开发资源

Many Android users have disabilities that require them to interact with their Android devices in different ways. These include users who have visual, physical or age-related disabilities that prevent them from fully seeing or using a touchscreen. Android provides accessibility features and services for helping these users navigate their devices more easily, including text-to-speech, haptic feedback, trackball and D-pad navigation that augments their experience. Android application developers can take advantage of these services to make their applications more accessible and also build their own accessibility services.

这个指南是为了让你的应用更容易访问:让应用更容易访问

Making sure your application is accessible to all users is relatively easy, particularly when you use framework-provided user interface components. If you only use these standard components for your application, there are just a few steps required to ensure your application is accessible: Label your ImageButton, ImageView, EditText, CheckBox and other user interface controls using the android:contentDescription attribute. Make all of your user interface elements accessible with a directional controller, such as a trackball or D-pad. Test your application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls.