android - android :contentDescription ="@null" and tools:ignore ="ContentDescription"?有什么区别使用android :importantForAccessibility ="no"?

标签 android android-layout attributes imageview lint

以这个 android 布局 XML fragment 为例:

<ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/bitcoin"
        android:contentDescription="@null"
        android:importantForAccessibility="no"
        tools:ignore="ContentDescription" />

android:contentDescription="@null"tools:ignore="ContentDescription"有什么区别?

我知道它们都用于表示某个非文本元素没有任何意义,仅用于装饰。使用一个比另一个有优势吗?我应该同时使用两者吗?是偏好,还是一个被认为比另一个更好和更新?

此外,我应该使用 android:importantForAccessibility="no" 还是使用所有三个属性/属性只是过度杀伤力?

最佳答案

What is the difference between android:contentDescription="@null" and tools:ignore="ContentDescription"?

android:contentDescription="@null"

  • 用于表示某个非文本元素没有任何意义,仅用于装饰。

tools:ignore="ContentDescription"

  • 用于图形元素,例如 ImageViewImageButton。如果您没有设置它们各自的 android:contentDescription XML 属性,将显示一条 lint 警告消息。

    “图像上缺少 contentDescription 属性”

  • 要抑制此 lint 警告消息,您必须在 XML 中使用 tools:ignore="ContentDescription"

I know both of them are used to indicate that a certain non-textual element carries no meaning and is only meant for decoration. Is there an advantage of using one over the other, should I use both, is it preference, or is one considered better and newer than the other?

不,它们在用法上彼此不同,例如

<ImageView 
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:id="@+id/image_user_avatar"
    android:contentDescription="User avatar"
    tools:ignore="ContentDescription" />

使用 TalkBack 运行应用时,它会说出“用户头像”。

Should I use android:importantForAccessibility="no"?

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

更新

So basically tools:ignore="ContentDescription" is only for the compiler and android:contentDescription="@null" is for user user?

是的,是的。

Also, my 'minSdk' is 14 and my 'targetSdk' is 28. Can I still set both android:importantForAccessibility="no" and android:contentDescription="@null"?

是的,您可以同时设置它们,但是如果您在 SDK 低于 16 的设备上运行应用程序,android:importantForAccessibility="no" 将被忽略。

Will android:contentDescription="@null" have the same effect as android:importantForAccessibility="no" for devices running Android 4.1 or higher?

它们略有不同。

android:contentDescription="@null":当用户在其上移动手指时,具有此属性的 View 仍会高亮显示,辅助功能服务会大声读出“按钮”等虚拟文本

android:importantForAccessibility="no":具有此属性的 View 已被应用禁用,因此当用户将手指放在上面时,它不会突出显示,也不会被辅助服务忽略。

关于android - android :contentDescription ="@null" and tools:ignore ="ContentDescription"?有什么区别使用android :importantForAccessibility ="no"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855426/

相关文章:

android - 从 JSON 显示布局中的单个对象

布局中间的 Android 选项卡

Android - 具有多个位图的Imageview

attributes - 您是否经常使用链接关系 ("rel"属性)?

C#使类自动注册

android - 在 Android 中测试 AutoCompleteTextView

android - 如何将值传递给 Android 上的 Facebook 对话框

java - 如何在 Fragment 中实现自动图像 slider

java - Invalidate() 不刷新屏幕

XPath:获取具有位置和属性的元素