我从Android Programming_的“大书呆子农场指南”一书中复制了第一个程序。我得到了下面的错误。在此行找到多个批注:- error:在文档元素后解析XML时出错: junk -文档中跟在根元素后面的标记必须是格式正确的。在第16行,下面是我的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QuizActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
andriod:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
</LinearLayout>发布于 2013-12-27 12:24:25
您正尝试在同一个xml中使用两个父布局。这在Android中是不可能的。在这种情况下,使用RelativeLayout或LinearLayout作为父对象。
使用此模板:
<RelativeLayout
//relative layout attributes
>
//you can add as many as elements here
<LinearLayout
//linear layout attributes
>
//you can add as many as elements here
</LinearLayout>
</RelativeLayout>如果您想同时使用RelativeLayout和LinearLayout,请在xml文件中添加单独的父布局,如下所示:
<RalativeLayout
//this is your main parent layout
>
//Add as many as Linear/Relative layout here doesn't matter.
</RelativeLayout>发布于 2013-12-27 12:13:28
更改布局:您正尝试为一个xml文件初始化两个独立的布局。使用一个主布局&您可以使用其他布局作为其子布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QuizActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
//u can specify `below` , `above` property as per your need
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
andriod:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>发布于 2013-12-27 13:43:50
// try this way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".QuizActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
</LinearLayout>https://stackoverflow.com/questions/20794588
复制相似问题