Java Android Studio 中的多个根标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24275533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:14:36  来源:igfitidea点击:

Multiple root tags in Android Studio

javaandroidandroid-layoutandroid-fragments

提问by rakeshdas

I am edit my fragment_main.xml in Android Studio, and I am getting this error:

我正在 Android Studio 中编辑我的 fragment_main.xml,但出现此错误:

Multiple Root Tags

多个根标签

The code block in question here is:

这里有问题的代码块是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>
<EditText   <!--Error here in the bracket-->
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button    <!--Error here in the bracket-->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>

I am getting the errors in the brackets before EditTextand before Button

我在EditText之前和Button之前收到括号中的错误

采纳答案by Anil Jadhav

Since in Android every xml file there must be only one root layout.Just add the EditText and Button inside the LinearLayout. Right code is shown below

由于在 Android 中,每个 xml 文件都必须只有一个根布局。只需在 LinearLayout 中添加 EditText 和 Button 即可。正确的代码如下所示

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText  
        android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />

</LinearLayout>