Android Lint contentDescription警告

问题描述 投票:125回答:10

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

那是什么意思?

android android-layout android-lint
10个回答
166
投票

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

android:contentDescription="@string/desc"

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

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

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


0
投票

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

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

没有更多的警告!快乐的编码


49
投票

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

android:contentDescription="@null"

37
投票

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

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" />

21
投票

我建议你添加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(graph,textAsImage,...)

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

使用内容说明对所有其他内容无用。


12
投票

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

  1. 单击右上角的红色按钮
  2. 选择“禁用问题类型”(例如)

1
投票

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


1
投票

ContentDescription需要Android辅助功能。特别适用于屏幕阅读器功能。如果您不支持Android辅助功能,可以使用setup Lint忽略它。

所以只需创建lint.xml

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

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

</lint>

并把它放到app文件夹。

enter image description here


0
投票

由于我需要ImageView添加一个仅用于美学的图标,我在我的xml文件中的每个ImageView中添加了tools:ignore="ContentDescription"

我不再收到任何错误消息


0
投票

对于纯粹装饰的图形元素,将其各自的android:contentDescription XML属性设置为“@null”。

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

© www.soinside.com 2019 - 2024. All rights reserved.