0

我想添加一些在 xml 资源文件的 ImageButton 中找不到的额外数据,并能够通过代码读取它

就这样

<ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            app:angle="0"
            app:titletext="imageButton1"/>

我在哪里可以将这些数据定义为可访问且不会导致 XML 错误

提前致谢

4

3 回答 3

1

我认为这在 Android SDK 中是不可能的。因为ImageButton元素需要预先定义。

你需要定义什么样的数据,为什么要在xml中定义?strings.xml还不够吗?

于 2012-02-12T21:56:52.023 回答
1

在 Android 中有一种定义的方式来做到这一点,即在 XML 中定义自己的命名空间,然后通过读取创建对象时传入的属性来读取代码中的值。

我不打算详细讨论这个问题,因为这个问题之前已经被问过,并且有很多关于它的好文章。

您可能想阅读以下文章:

于 2012-02-12T22:15:21.590 回答
0

无需在 ImageButton 中查找隐藏数据,您可以将其子类化并拥有自己的数据字段,如果您仍想使用 ImageButton,请为其提供一个包装类,其中包含一个 ImageButton 作为字段变量,如下所示:-

class ButtonWrapper{
   ImageButton button;
   int app_angle=0;
   String app_titletext="imageButton1"
}
于 2012-02-12T22:10:36.453 回答