Android自定义视图XML中的复杂属性
Android自定义视图XML中的复杂属性,android,xml,view,android-custom-view,Android,Xml,View,Android Custom View,我有一个关于自定义视图XML声明的问题。 我用自定义属性创建了自己的视图。现在我想添加更复杂的属性,如下所示:(这是非工作代码) 也许这会在某个时候对某人有所帮助 除非CustomTextView扩展ViewGroup(或将其包含在其类层次结构中)并且Animation是您定义的自定义视图,否则这将不起作用。布局XML文件中只允许视图和视图组(Android定义的一些特殊标记,如包括和合并),并且只有视图组元素可以有子元素。您可以向自定义视图添加自定义XML属性,如下所示: <reso
我有一个关于自定义视图XML声明的问题。
我用自定义属性创建了自己的视图。现在我想添加更复杂的属性,如下所示:(这是非工作代码)
也许这会在某个时候对某人有所帮助 除非CustomTextView
扩展ViewGroup
(或将其包含在其类层次结构中)并且Animation
是您定义的自定义视图,否则这将不起作用。布局XML文件中只允许视图
和视图组
(Android定义的一些特殊标记,如包括
和合并
),并且只有视图组元素可以有子元素。您可以向自定义视图添加自定义XML属性,如下所示:
<resources>
<declare-styleable name="YourCustomClass">
<attr name="someCustomAnimationAttribute" format="reference" />
</declare-styleable>
<resources>
<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"
app:someCustomAnimationAttribute="@drawable/someAnimation">
然后像这样使用它:
<resources>
<declare-styleable name="YourCustomClass">
<attr name="someCustomAnimationAttribute" format="reference" />
</declare-styleable>
<resources>
<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"
app:someCustomAnimationAttribute="@drawable/someAnimation">
通过这种方式,您可以通过XML提供这些动画,而不是将它们作为子元素添加,这是不可能的。您应该为此创建自定义XML属性,如这里所述:我认为OP的主要关注点是如何在自定义视图中检索子XML标记,即@Karakuri指出的
,如果该自定义视图扩展了ViewGroup
,则可以添加子视图(具有自定义参数的自定义子视图)。但是,以这种方式提供动画是行不通的。没错。我已经知道如何定义简单属性。问题是是否可以将更复杂的属性定义为XML子元素。不过还是要谢谢你!好的,谢谢!这正是我所想的。我只是希望有一种我还没见过的方法。
xmlns:app="http://schemas.android.com/apk/res-auto"
<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"
app:someCustomAnimationAttribute="@drawable/someAnimation">