Android Lint contentDescription警告

San*_*gar 126 android android-layout android-lint

我收到警告,因为"[可访问性]在图像上缺少contentDescription属性"for imageview.而使用android lint

那是什么意思?

San*_*gar 167

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

android:contentDescription="@string/desc"
Run Code Online (Sandbox Code Playgroud)

ADT 16中的Android Lint支持会抛出此警告,以确保图像窗口小部件提供contentDescription.

这定义了简要描述视图内容的文本.此属性主要用于辅助功能.由于某些视图没有文本表示,因此该属性可用于提供此类属性.

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

  • 您可以阅读更多相关信息并自行测试,请访问:http://android-developers.blogspot.com/2012/04/accessibility-are-you-serving-all-your.html和http:// developer .android.com /引导/主题/ UI /无障碍/ apps.html#测试 (4认同)

Dan*_*ite 49

禁用Lint警告很容易让您以后遇到麻烦.你最好只为所有的ImageView指定contentDescription.如果您不需要描述,那么只需使用:

android:contentDescription="@null"
Run Code Online (Sandbox Code Playgroud)


Gun*_*ein 37

另一种选择是单独抑制警告:

xmlns:tools="http://schemas.android.com/tools"  (usually inserted automatically)
tools:ignore="contentDescription"
Run Code Online (Sandbox Code Playgroud)

例:

<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" />
Run Code Online (Sandbox Code Playgroud)

  • 这是一个日食问题.只需清理你的项目.并确保:xmlns:tools ="http://schemas.android.com/tools"也包括在内! (3认同)

Chr*_*ist 23

我建议你添加contentDescription.

android:contentDescription="@string/contentDescriptionXxxx"
Run Code Online (Sandbox Code Playgroud)

但是,让我们现实一点.大多数人不保持文字的可访问性.尽管如此,只需付出很少的努力,您就可以实施一些措施来帮助残障人士.

<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>
Run Code Online (Sandbox Code Playgroud)

.

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

使用contentDescriptionAction进行任何可点击的操作.

对包含信息的图像使用contentDescriptionContent(graph,textAsImage,...)

对所有用户提供的内容使用contentDescriptionUserContent.

所有其余的使用contentDescriptionUseless.


Ser*_*iro 12

因为它只是一个警告,你可以抑制它.转到XML的图形布局并执行以下操作:

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

    在此输入图像描述

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

    在此输入图像描述

  • 没错,你可以解压缩它,但是你可能应该遵循所选答案的建议,以便依赖于Android提供的辅助工具的用户. (4认同)

Gia*_*ran 8

转到Gradle文件(模块应用程序),添加以下代码块

android {
    ... 
    lintOptions {
        disable 'ContentDescription'
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

不再有警告!快乐编码


Ale*_*nat 7

如果您想以优雅的方式抑制此警告(因为您确定此特定 ImageView 不需要可访问性),您可以使用特殊属性:

android:importantForAccessibility="no"
Run Code Online (Sandbox Code Playgroud)