【问题标题】:Android: Define a view and its children once and reuse it anywhereAndroid:定义一次视图及其子项,并在任何地方重用它
【发布时间】:2015-05-09 03:13:30
【问题描述】:

我想定义一次以下 xml,然后在任何活动中重用它。我刚刚读到我可以使用样式,但示例here 仅设置没有子视图的单个视图。我知道我可以夸大它,但是否可以纯粹在 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>

【问题讨论】:

标签: android xml code-reuse


【解决方案1】:

使用include 标签。请参阅this 指南。

编辑

另见this答案

【讨论】:

  • 这适用于我给出的具体示例,但如果最外面的视图不是布局,我想知道如何执行此操作。暂时给你点赞。
  • 我认为您正在尝试引用 merge 标签。 在上面的链接中也有说明
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多