【问题标题】:Android app tutorial content.xml and activity.xml files have different code to tutorialAndroid 应用教程 content.xml 和 activity.xml 文件与教程有不同的代码
【发布时间】:2016-01-28 06:14:35
【问题描述】:

我正在学习 android 教程 http://developer.android.com/training/basics/firstapp/building-ui.html

我已经按照教程中的说明创建了项目,但是它所说的代码应该在我的 activity.xml 文件中,而不是在我的 content.xml 文件中,就好像它们已经交换了一样?

活动 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=".MyActivity">

<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_my" />

<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>

内容 xml 中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

教程告诉我将相对布局块更改为线性布局(它说应该在活动 xml 中,但对我来说它在内容 xml 中)

因此,当我用教程中的内容替换活动 xml 中的代码时,它给了我错误,因为我删除了之前的代码。

如果我在内容 xml 中编辑代码并运行应用程序,我看不到更改,因为活动未更新,并且我无法将线性布局代码移动到活动 xml,因为它给了我“多个根标签”错误。

谁能告诉我一个解决方案,让我继续学习本教程,因为如果我编辑 java 代码或类似的东西,我会在教程的后面阶段遇到问题。

谢谢

【问题讨论】:

  • 我已经为此添加了一个新答案...对于尝试在 android 上制作他们的第一个应用程序的初学者来说更有意义
  • 感谢您发布这两个脚本...我的res/layout/ 根本没有content_my.xml...所以我复制了您的代码...

标签: java android xml android-studio


【解决方案1】:

在环顾了一段时间并尝试了多种解决方案后,我终于意识到在 activity_my.xml 中创建的工具栏遮挡了在 content_my.xml 中创建的文本/按钮。

我的解决方案是填充线性布局。

android:paddingTop="55dp"

但话说回来,我在 Android/Java 世界的第 2 天...

【讨论】:

    【解决方案2】:

    因此,如果您按照教程进行操作,即使您的 activity_my.xml 文件与他们所期望的完全相同,那么您将看到编辑文本视图和按钮视图描绘。

    自创建本教程以来,空白活动模板似乎已经添加了相当多的内容,使教程变得晦涩难懂。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。从来没有真正使用过 XML,而且我的 Java 已经生锈了。我最终在网上搜索了一堆,这就是我发现的。

      • .xml 文件用于确定活动的布局。它们可以相互结合使用,也可以单独使用。这意味着使用 content.xml 和 main_activity.xml 并不重要。但是,由于教程对此不是很清楚,因此新手很难找到解决方法。

      • 解决方案是了解 MainActivity.java 文件对 .xml 文件的作用。 i-programmer 的 Mike James 解释得很好...(http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html)

      • 有一个名为 onCreate() 的方法可以控制 MainActivity.java 使用的布局。在这里,您将看到自动生成的函数 setContentView(),这是确定应用中显示内容的主要问题。

      • 还有一堆其他生成的代码用于从 main_activity.xml 获取更多数据。如果您查看代码,您将看到需要删除的内容。

      希望这会有所帮助。如果有,请投票:)

      【讨论】:

        【解决方案4】:

        您只需要记住,这些 .xml 文件代表布局部分,即 UI。 简而言之,Android 中的“屏幕”由 2 个文件组成:布局(.xml 文件)及其关联的 .java 文件。 在这里,我们可以看到您的“内容”包含在您的“活动”中。 这与将“content.xml”的内容直接放入“activity.xml”中是完全一样的。 MyActivity.java 的“OnCreate()”方法是什么?

        您是否尝试用“content.xml”的实际内容替换“activity.xml”中的“include”,不要忘记删除“根特定”值(xmlns:android、xmlns:tools、xmlns:app 和工具:上下文)?

        正如 fernaMuruthi 所说,请尝试遵循 Android 入门资源。另外,尽量不要使用图形设计器:它有时会生成蹩脚的代码。而是直接输入你的 UI 伪 xml。

        【讨论】:

          【解决方案5】:

          尝试编辑 content.xml 以反映 LinearLayout 更改(如图所示添加方向值),而不是处理 activity.xml。

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
              android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              tools:showIn="@layout/activity_main" tools:context=".MainActivity">
          
              <TextView android:text="Hello World!" android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
          </LinearLayout>
          

          也请浏览 Android Getting Started 资源。他们会派上用场

          【讨论】:

          • 是的,但是当我开始添加按钮和东西时,它会停止反映在活动 xml 中?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-26
          • 1970-01-01
          • 2012-09-17
          相关资源
          最近更新 更多