Android应用教程content.xml和activity.xml文件有不同的教程代码

时间:2015-10-28 12:53:58

标签: java android xml android-studio

我正在研究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)

因此,当我将activity xml中的代码替换为教程所具有的内容时,它会给我带来错误,因为我删除了以前的代码。

OR

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

有人可以向我展示一个允许我继续学习本教程的解决方案吗,因为如果我编辑java代码或类似内容,我会在教程的后续阶段遇到问题。

由于

4 个答案:

答案 0 :(得分:1)

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

   
<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资源。他们会派上用场的

答案 1 :(得分:1)

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

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

正如fernaMuruthi所说,请尝试关注Android入门资源。另外,尽量不要使用图形设计器:它有时会产生糟糕的代码。相反,直接键入UI伪xml。

答案 2 :(得分:1)

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

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

  • 解决方案是了解MainActivity.java文件对.xml文件的作用。 i-programmer的迈克詹姆斯解释得相当好......(http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html

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

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

希望这会有所帮助。如果确实如此,请进行投票:)

答案 3 :(得分:0)

看了一会儿并尝试了多个解决方案后,我终于意识到activity_my.xml中创建的工具栏模糊了在content_my.xml中创建的文本/按钮。

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

android:paddingTop="55dp"

但是我又在Android / Java世界的第二天......