2017-08-10 40 views
-2

这就是我的意思如何使用多个视图组在一个XML纸张

<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> 
+0

XML文件只能有一个根元素。所以答案可能不是。我已经添加了 –

回答

0

当然可以,但是, 您没有添加的高度和宽度的RelativeLayout和LinearLayout中。这就是为什么你有错误。 添加类似这样的内容android:layout_heightandroid:layout_width 即使您需要将这些添加到像LinearLayout这样的单个rootView中。

+0

。以上代码仅用于说明目的 – Nicholas

+0

现在您看到了我编辑的答案 –

+0

是的。我是否只需要为根标记添加分离高度和宽度? – Nicholas

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> 
+0

标签内部是否会受到我选择作为根标签的布局类型的影响? – Nicholas

+0

定位和定位将受到根级布局属性的影响。 –

+0

特别感谢您的帮助 – Nicholas