Android:一次定义一个 View 及其 subview 并在任何地方重复使用它

标签 android xml code-reuse

我想一次定义以下 xml,然后在任何 Activity 中重用它。我刚刚读到我可以使用样式,但是示例 here只为没有 child 的单个 View 设置样式。我知道我可以膨胀这个,但是否可以纯粹在 xml 中做到这一点?

<RelativeLayout
android:id="@+id/group_chat_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:background="@drawable/transparent_background2" >

    <TextView 
    android:id="@+id/send_msg_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/transparent_background2"
    android:text="Send" />

    <EditText
    android:id="@+id/group_chat_input_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/send_msg_button"
    android:layout_toLeftOf="@id/send_msg_button"
    android:background="#00000000"
    android:hint="@string/type_a_message"
    android:textColorHint="#EEEEEE"
    android:inputType="textMultiLine"
    android:maxLines="5"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:shadowColor="#000000"
    android:shadowRadius="3"
    android:shadowDx="3"
    android:shadowDy="3"/> 

</RelativeLayout>

最佳答案

使用 include 标签。参见 this指导。

编辑

另见 this回答

关于Android:一次定义一个 View 及其 subview 并在任何地方重复使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917020/

相关文章:

java - 使用 XPath 获取组中的节点值

javascript - 使用 require() 在包含的 javascript 中使用函数

android - 为什么? AndroidStudio + Gradle = 46000方法计数VS Eclipse + Ant = 43000方法计数

android - Google Maps v2 设置缩放级别

java - android.os.TransactionTooLargeException 检索已安装的应用程序

tags - 在 jar 中重用 jSTL 标签

java - Android 中的函数重用

java - 获取当前 Android 设备时间(如果是手动设置的)

android - 如何删除 firestore 中具有不同 id 的多个文档?

javascript - NativeScript Sidekick 是否会在构建应用程序时删除 XML 和 JavaScript 中的注释并缩小源代码?