简体   繁体   中英

How to eliminate warning in android layout.xml: "missing contentDescription"

<ImageView style="@style/LoaderPrevNext.Next" />

using the styles

<style name="LoaderPrevNext">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">80dp</item>
    <item name="android:layout_weight">0</item>
    <item name="android:scaleType">fitXY</item>
</style>

<style name="LoaderPrevNext.Next">
    <item name="android:src">@drawable/next_page</item>
    <item name="android:contentDescription">@string/img_desc_next_page</item>
</style>

annoys me with the [Accessibility] Missing contentDescription attribute on image warning.

It disappears if I move or copy the contentDescription from the style to the ImageView tag, but as the src is defined in the style, I'd love to keep them together.

Guess it's simply an SDK bug, but some might got a workaround...

Clarification: The ImageView in question do have a content-description, defined in the LoaderPrevNext.Next style. The warning is false. I'm not interested in ideas on how to set the content description, or how to hack them empty.

在 Eclipse Window->Preferences->Android->Lint Erroe Checking->Right Side Scroll Down until Accessibility->Content Description->Severity->Select Ignore->Apply->yes

How To Suppress Android Accessibility Warning Missing contentDescription

Both of the existing answers to this question are technically correct, but I believe there is a better way. The first answer suggests turning off all contentDescription warnings. While this works, contentDescription is there for a reason, and perhaps should not be globally turned off for all content.

The second answer shows how to specify the contentDescription . While this does make the warning go away, it is not appropriate for all content and technically does not answer the question, although it is a valid work-around.

The Android documentation for lint provides the best solution, IMO, which is a mixture of providing contentDescription for some content, and suppressing the lint warning for other content:

Note that elements in application screens that are purely decorative and do not provide any content or enable a user action should not have accessibility content descriptions. In this case, just suppress the lint warning with a tools:ignore="ContentDescription" attribute.

Therefore, Implement the following solution to remove all of the lint warnings:

  • Define contentDescription in your XML layout file with android:contentDescription="Your description here." for resources that provide interesting or useful information to the user. And,
  • Add tools:ignore="ContentDescription" to purely decorative content or images.

To disable missing content description warning in the whole project, add this to your build.gradle

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}

If you do not wish to turn off / ignore the lint warnings you could define an empty string in strings.xml

string.xml:

<string name="empty"></string>

and then in your xml just set the value

<ImageView
    android:id="..."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:contentDescription="@string/empty"/>

contentDescription tag is used for screen reading feature of android. you can add a string about what is your image is or you can just suppress/ignore this warning by adding the following tag.

tools:ignore="ContentDescription"

< ImageView

android:id= ...

android:layout_width= ... ...

/>

Why would you want to have image description in Style.

The purpose of styles is to have an ability to change something in one place which is reflected in several places.

Same goes for @string resource. Simply don't put image description in style. Leave @string. Once needed - change that string itself.

I think that the correct solution is to report this as a bug in Android Studio, though I doubt that they will fix it anytime soon (from experience with my bug reports about other things). https://developer.android.com/studio/report-bugs

I'm sorry that so far you have had only off-topic answers that totally ignore the key elements of your question, and answers that claim that styles aren't meant for doing what you want to them (even though the docs never warn against what you are trying to do). I came here because I have the same problem as you have (in 2023 and which I encountered around 2020). I don't see any sign of Android Studio / Android Lint offering an option for taking styles into account. It doesn't even look like there is a point to not taking styles into account, therefore there shouldn't be an option for this, any rule that checks presence or content of specific attributes should also look up the style specified by style="".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM