0

我一直在用谷歌搜索这个主题,但没有找到相关信息。我很清楚如何扩展视图,但我不想扩展任何东西。

我想用自定义属性稍微“注释”哪个android视图(或任何视图的后代),然后在运行时检索它们的值。

像这样:

    <TextView
    custom:my_property_name="foo here"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_view"
    android:text="Rule" />

然后,一旦我引用了这个 TextView,我想调用一个方法,例如:

    String myProperty = textView.getProperty("my_property_name");
    myProperty.equals("foo here");

这可能吗?如何?

谢谢。

4

1 回答 1

0

至少您必须创建自己的类,该类是标准 View 类之一(或 View 本身)的子类。现有框架代码不会读取 Android 未定义且不属于该视图的可样式声明的属性。

Android 文档有一个页面描述了使用自己的 XML 属性创建自己的视图:http: //developer.android.com/training/custom-views/create-view.html

于 2014-08-26T15:17:16.377 回答