XML中“缺少图像上的contentDescription属性”

时间:2012-03-16 01:34:13

标签: android eclipse warnings accessibility android-imageview

我在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>

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

9 个答案:

答案 0 :(得分:256)

请点击此链接获取解决方案: Android Lint contentDescription warning

  

通过设置属性android:contentDescription解决了此警告   对于我的ImageView

     

机器人:contentDescription = “@串/ desc” 的

     

ADT 16中的Android Lint支持会抛出此警告以确保这一点   图像小部件提供contentDescription

     

这定义了简要描述视图内容的文本。这个   property主要用于可访问性。由于一些观点没有   具有文本表示,该属性可用于提供   这样

     

像ImageViews和ImageButtons这样的非文本小部件应该使用   contentDescription属性指定的文本描述   窗口小部件,屏幕阅读器和其他辅助工具可以   充分描述用户界面。

此链接有解释: Accessibility, It's Impact and Development Resources

  

许多Android用户都需要他们进行互动   以他们的Android设备以不同的方式。这些包括用户   有视觉,身体或年龄相关的残疾,以防止他们   从完全看到或使用触摸屏。

     

Android提供辅助功能和服务来帮助他们   用户可以更轻松地浏览他们的设备,包括文本到语音,   触觉反馈,轨迹球和D-pad导航增强他们的   经验。 Android应用程序开发人员可以利用这些优势   服务使他们的应用程序更易于访问和构建   他们自己的无障碍服务。

本指南适用于您的应用访问: Making Apps More Accessible

  

确保所有用户都可以访问您的应用程序是相对的   很容易,特别是当您使用框架提供的用户界面时   组件。如果您只使用这些标准组件   应用程序,只需几个步骤来确保您的   应用程序可访问:

     
      
  1. 标记 ImageButton ImageView EditText CheckBox 和其他用户   界面控件使用 android:contentDescription 属性。

  2.   
  3. 使用方向访问所有用户界面元素   控制器,如轨迹球或D-pad。

  4.   
  5. 通过启用TalkBack等辅助功能服务来测试您的应用程序   和触摸探索,   并尝试仅使用方向控件来使用您的应用程序。

  6.   

答案 1 :(得分:65)

android:contentDescription="@string/description"(静态或动态)添加到ImageView。 请不要忽略或过滤消息,因为它有助于使用替代输入方法的人因为他们的残疾(如TalkBack,Tecla Access Shield等)。

答案 2 :(得分:33)

<强>更新

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

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

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

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

我使用的tools:ignore="ContentDescription"就是这样的意思。

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

答案 3 :(得分:9)

警告确实很烦人,在许多(大多数!)情况下,各种装饰性ImageView都不需要contentDescription。解决问题最根本的方法就是告诉Lint忽略这个检查。在Eclipse中,转到&#34; Android / Lint错误检查&#34;在首选项中,找到&#34; contentDescription&#34; (它位于&#34;辅助功能&#34;组)并更改&#34;严重性:&#34;忽略。

答案 4 :(得分:9)

如果你根本不在乎这样做:

    android:contentDescription="@null"

虽然我会建议接受的解决方案,但这是一个黑客攻击:D

答案 5 :(得分:8)

添加

tools:ignore="ContentDescription"

到你的形象。确保您的根布局中有xmlns:tools="http://schemas.android.com/tools" .

答案 6 :(得分:2)

这是警告您,因为未定义图像描述。

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

在Strings.xml下面添加此行

address

也将此行添加到activity_main.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" />

Strings.xml

android:contentDescription="@string/imgDescription"

答案 7 :(得分:0)

展望未来,对于纯装饰的图形元素,最好的解决方案是使用:

android:importantForAccessibility="no"

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

如果您坚持支持较旧的版本,则应使用(就像已经指出的其他版本一样):

android:contentDescription="@null"

来源:https://developer.android.com/guide/topics/ui/accessibility/apps#label-elements

答案 8 :(得分:0)

此警告试图改善您的应用程序的可访问性。

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

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}
相关问题