Android 如何在单个XML工作表中使用多个视图组
这就是我的意思Android 如何在单个XML工作表中使用多个视图组,android,android-layout,android-studio,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
<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>
/*都在同一个XML表中*/
当我试着那样做的时候,它给了我错误。还有别的选择吗
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>
是的,你可以,但是,
您尚未为RelativeLayout和LinearLayout添加高度和宽度。这就是为什么你有错误。
添加如下内容:android:layout\u height和android:layout\u width
甚至您也需要将它们添加到一个单独的根视图中,如LinearLayout或类似的东西。因此有一个问题。根据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>
/*都在同一个XML表中*/
我已经添加了它。上面的代码只是为了举例说明。我是否只需要为根标记添加一个单独的高度和宽度?当您选择布局时,视图组需要按照根标记进行定位。选择我的答案作为正确答案并完成此主题。我仍然可以用于此目的。您是否可以向我发送一个虚拟代码,说明应该如何设置。我在这里遇到了一系列错误,XML文件只能有一个根元素。所以答案可能不是。里面的标签会受到我选择作为根标签的布局类型的影响吗?方向和定位会受到根级别布局属性的影响。