【发布时间】:2014-04-30 08:42:42
【问题描述】:
我有一个关于自定义视图 XML 声明的问题。
我像往常一样使用自定义属性创建了自己的视图。现在我想添加更复杂的属性,如下所示:(这是非工作代码)
<com.xxx.yyy.CustomTextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/customTextView1"
android:layout_marginBottom="22dp"
android:layout_toRightOf="@+id/buttonBlack"
android:text="TextView" >
<Animation
animation:property1="123"
animation:property2="456" />
<Animation
animation:property1="789"
animation:property2="012" >
</Animation>
</com.xxx.yyy.CustomTextView>
我自己没有找到办法,但也许有人有想法。
谢谢!
编辑:
我只是或多或少地很好地解决了这个问题。 我在名为 animations.xml 的 /res/xml 文件夹中创建了一个新的 .xml 文件
<animations>
<animation
name="Animation name 1"
float1="1.1"
float2="1.2"
integer1="11"
integer2="12" />
<animation
name="Animation name 2"
float1="2.1"
float2="2.2"
integer1="21"
integer2="22" />
</animations>
我在 attrs.xml 中的自定义视图包含一个用于从上面引用 animations.xml 文件的属性:
<declare-styleable name="MyTextView">
<attr name="animations" format="reference" />
</declare-styleable>
现在我在 MyTextView 的构造函数中解析引用的 .xml 文件,如下所述:http://thedevelopersinfo.com/2009/12/14/using-xml-file-resources-in-android/
也许这有时会帮助某人。
【问题讨论】:
-
您应该为此创建自定义 XML 属性,如下所述:developer.android.com/training/custom-views/…
标签: android xml view android-custom-view