如何在单个XML表单中使用多个View组

时间:2017-08-10 03:23:23

标签: android android-layout android-studio

这就是我的意思

<RelativeLayout>
    <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</RelativeLayout>
/*ALL IN THE SAME XML SHEET*/
<LinearLayout>
    <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

当我尝试这样做时,它给了我错误。有没有其他选择

Anuj Kumar要求的代码。我已经尝试交换xmlns属性的位置

<?xml version="1.0" encoding="utf-8"?>
<linearLayout 
      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:orientation="portrait">

     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.ifeoluwa.mcdonaldsapp.MainActivity">
             <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Hello!"/>
   </RelativeLayout>

</linearLayout>

2 个答案:

答案 0 :(得分:0)

是的,你可以,但是, 你还没有为RelativeLayout和LinearLayout添加高度和宽度。这就是你有错误的原因。 添加类似android:layout_heightandroid:layout_width的内容 即使您需要将这些添加到单个rootView中,如LinearLayout或类似的东西。

答案 1 :(得分:0)

所以有一个问题。根据XML限制,只有一个根,但您可以在根目录中有多个子节点。因此,您的XML代码需要更改如下。

<LinearLayout>
  <RelativeLayout>
      <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
  </RelativeLayout>
/*ALL IN THE SAME XML SHEET*/
  <LinearLayout>
      <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

  </LinearLayout>
</LinearLayout>