Android 如何将子类与XML文件关联?

Android 如何将子类与XML文件关联?,android,xml,subclass,Android,Xml,Subclass,我有一个XML文件,描述了一个包含按钮、图像和文本的对话框。为了覆盖onClick并为对话框中的所有小部件调用它,我编写了一个扩展RelativeLayout的子类。现在我想将XML文件与子类相关联。我可以不用充气机吗 <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <

我有一个XML文件,描述了一个包含按钮、图像和文本的对话框。为了覆盖onClick并为对话框中的所有小部件调用它,我编写了一个扩展RelativeLayout的子类。现在我想将XML文件与子类相关联。我可以不用充气机吗

 <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:clickable="true"
                android:duplicateParentState="true"
                android:onClick="onClick"
                android:src="@drawable/ic_launcher" />

            <EditText
                android:id="@+id/TextView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:duplicateParentState="true"
                android:ems="10"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:inputType="textMultiLine"
                android:text="text" >

                <requestFocus />
            </EditText>

        </RelativeLayout>

谢谢,
西蒙

当然。使用子类的完整包名,而不是键入RelativeLayout

  <com.example.MySubclassRelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
      ...
  </com.example.MySubclassRelativeLayout >

...

当然可以。使用子类的完整包名,而不是键入RelativeLayout

  <com.example.MySubclassRelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
      ...
  </com.example.MySubclassRelativeLayout >

...

您可以将这些小部件放入一个布局中,例如dialogwidgets.xml,然后您可以使用include标记插入您想要的所有布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/dialogwidgets"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

...

您可以将这些小部件放入一个布局中,例如dialogwidgets.xml,然后您可以使用include标记插入您想要的所有布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/dialogwidgets"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>

...

显示XML文件-不清楚您想要实现什么。显示XML文件-不清楚您想要实现什么。