5

虽然常量contentDescription适用于某些小部件,但其他情况(例如带有标题的图片库)已经在 UI 中具有适当的可访问性标签。有没有办法将ImageView'scontentDescription与其Textview在 XML 中的标签联系起来?

例如,此ImageView定义的最后(虚构)行是否存在真正的等价物:

<LinearLayout>
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@+id/label" />
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
4

1 回答 1

13

如果您已经知道 的值,则TextView可以使用它:

<LinearLayout>
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/some_str" />
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/some_str" />
</LinearLayout>

但是,如果您以TextView编程方式设置 的内容,则需要对ImageView的内容描述执行相同的操作。(请参阅本文档。)

String desc = "Foo Bar";
yourTextView.setText(desc);
yourImageView.setContentDescription(desc);
于 2012-08-18T23:17:23.803 回答