【问题标题】:Difference between activity-(class).xml and content-(class).xml? [duplicate]activity-(class).xml 和 content-(class).xml 之间的区别? [复制]
【发布时间】:2016-07-01 11:48:41
【问题描述】:

嗨 =) 我只是好奇,当我在 Android Studio 中创建一个 android 项目时,我相信我有最新版本的,有一个空白活动或任何它构成主要活动的东西......而且这个主要活动带有 2 个 xml 文件,activity-main.xml 和 content-main.xml 好吧,我有几本参考指南和教程书籍都说导航到 activity-main.xml 以编辑实际应用程序的布局。但是activity-main.xml的内容包括:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="blahblahblah">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

我假设这仅适用于带有浮动操作按钮的较新的 android 版本,因为我可以看到的这个文件控制操作栏和操作按钮...但是其他所有内容的应用程序布局实际上包含在内容中-main.xml 我只是好奇将这些拆分为文件发生了什么?

【问题讨论】:

  • 你可以在旧的安卓版本中做同样的事情。 &lt;include&gt; 标签并不新鲜。它是为layout reuse 添加的。

标签: android xml


【解决方案1】:

据我所知,没有区别。 您可以删除两者并继续使用您自己的 'my-own-activity.xml'

需要的是以下两件事:

  1. 您的 xml 文件包含 xml 标头
  2. 您在活动onCreate() 的“setContent()”中设置了正确的 xml 文件

编辑:这里建议:https://stackoverflow.com/a/32880945/1750013 显然这两个 XML 致力于创建更易读的代码,但在功能方面没有区别,即都是纯 XML 文件。

【讨论】:

  • 略有不同。活动包括内容,它被设计用于应用程序的整体主题。您可以像 Fragments 一样切换内容并保持显示相同的 Activity。
  • @cricket_007 同意。但这只是为了可读性。可以随时删除这两个文件并手动创建一个新的 xml 文件
  • 啊我现在明白了。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-06-09
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多