android如何在xml中引用内部类

android如何在xml中引用内部类View

     

    比如在类com.example.ClassA中定义了一个类ViewB,那么怎么样在xml中引用ViewB呢?

    首先要注意的是ViewB定义的时候,必须定义成静态的,就是public static ViewB

    假设ViewB 继承自View,那么在xml中可以这样定义(ClassA和ViewB之间是"$",不是".")

    

<View
        class="com.example.ClassA$ViewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 

    假设ViewB继承自ViewGroup,那么在xml中可以这样定义(ClassA和ViewB之间是"$",不是".")

    <ViewGroup
        class="com.example.ClassA$ViewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <!-- ViewGroup内部的一些控件,比如button之类的 -->
    </ViewGroup>

猜你喜欢

转载自titanseason.iteye.com/blog/1831185