【问题标题】:Splitting one activity into several XML files?将一项活动拆分为多个 XML 文件?
【发布时间】:2021-03-09 02:52:57
【问题描述】:

所以问题是,我现在使用 Android Studio 编写一个非常简单的应用程序,它只有一个活动,但有几个视图,根据用户的需要出现和消失。一切看起来都很流畅,只是问题是所有的视图都写在同一个主要的活动布局 XML 文件中,这真的变得巨大而难以导航和跟踪。我想问的是,是否有任何方法可以减少此类问题,例如,将一个巨大的活动 XML 文件拆分为几个较短的文件,然后“组合”它们?

【问题讨论】:

  • 您可以创建多个 xml 文件,然后在主布局中使用 include 标签
  • 知道了!谢谢你!

标签: android android-layout android-activity android-xml


【解决方案1】:

您可以创建多个 xml 文件并包含在 xml 文件中。

活动布局文件:

<include
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   layout="@layout/yourlayout" />

子布局文件:名称为 yourlayout.xml

   <YourView
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

您可以参考以下网址。

https://developer.android.com/training/improving-layouts/reusing-layouts.html

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多