ImageView 的 xml 中的 android:contentDescription="@string/desc" 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11595828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is android:contentDescription="@string/desc" in ImageView's xml?
提问by foki
I added an imageView
in GraphicalLayout
but it looks different on my real device than on AVD.
我添加了一个imageView
inGraphicalLayout
但它在我的真实设备上看起来与在 AVD 上不同。
I found that I need to add contentDescription
in a layout .xml
, but when I add: contentDescription="@string/desc"
我发现我需要添加contentDescription
一个 layout .xml
,但是当我添加时:contentDescription="@string/desc"
there is an error:
有一个错误:
"No resource found that matches the given name (at 'contentDescription' with value '@string/desc')"
“找不到与给定名称匹配的资源(在 'contentDescription' 中,值为 '@string/desc')”
What is this string "desc"? What should it looks like?
这个字符串“desc”是什么?它应该是什么样子?
回答by starscream_disco_party
It's for the Accessibility Features of Android. The contentDescription
is what get's read back to the (assumingly blind or hard-of-sight user) so they have an idea of what the picture is since they aren't able to view it.
它用于 Android 的辅助功能。这contentDescription
是回读给(假设是盲人或视力不好的用户)的内容,因此他们知道图片是什么,因为他们无法查看它。
As to answer your question, @iturki gives the start of it. You write the string to use for the description in strings.xml
and give it a name
of 'desc' or whatever your heart desires, then you set the value of contentDescription
in the ImageView to @string/desc
, either in the layout or programmatically. Like I said before, it's to help users with poor eyesight get a general idea of what the image is portraying :)
至于回答你的问题,@iturki 给出了它的开始。您编写用于描述的字符串strings.xml
并给它一个name
'desc' 或任何您内心想要的东西,然后在布局中或以编程方式contentDescription
将 ImageView 中的值设置为@string/desc
。就像我之前说的,这是为了帮助视力不佳的用户大致了解图像所描绘的内容:)
回答by iTurki
You need to add it in your string.xml
file:
您需要将其添加到您的string.xml
文件中:
<string name="desc">your desc goes here</string>
Although I don't know why you NEED to add contentDescription
to your imageView to solve the inconsistent look problem! I don't think this will solve it.
虽然我不知道为什么你需要添加contentDescription
到你的 imageView 来解决不一致的外观问题!我不认为这会解决它。
回答by Sparky
That's for Accessibility (e.g., for a screen reader). You should define a string resource that describes the image, and reference it in contentDescription. See iturki's answer for how to do this.
那是为了辅助功能(例如,对于屏幕阅读器)。您应该定义一个描述图像的字符串资源,并在 contentDescription 中引用它。请参阅 iturki 的答案以了解如何执行此操作。
回答by Marg
Instead of using Content description and string description just use tools:ignore="ContentDescription" on your image view
不要使用内容描述和字符串描述,只需在图像视图上使用 tools:ignore="ContentDescription"
回答by eli
public void setContentDescription (CharSequence contentDescription)
Defines text that briefly describes content of the view.
public void setContentDescription (CharSequence contentDescription)
定义简要描述视图内容的文本。
A content description briefly describes the view and is primarily used for accessibility support to determine how a view should be presented to the user. In the case of a view with no textual representation, such as ImageButton, a useful content description explains what the view does. For example, an image button with a phone icon that is used to place a call may use "Call" as its content description. An image of a floppy disk that is used to save a file may use "Save".
内容描述简要描述了视图,主要用于可访问性支持,以确定应如何向用户呈现视图。在没有文本表示的视图的情况下,例如 ImageButton,有用的内容描述解释了视图的作用。例如,用于拨打电话的带有电话图标的图像按钮可以使用“呼叫”作为其内容描述。用于保存文件的软盘映像可以使用“保存”。