【发布时间】: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