Android 嵌套线性布局

Android 嵌套线性布局,android,android-linearlayout,Android,Android Linearlayout,我在Hello Android的书中读到了这段代码,我不知道为什么要写两行布局。 我可能是这些人中的一个,或者搬到另一个? 为什么要使用嵌套线性布局?它有什么作用 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/background"

我在Hello Android的书中读到了这段代码,我不知道为什么要写两行布局。 我可能是这些人中的一个,或者搬到另一个? 为什么要使用嵌套线性布局?它有什么作用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="30dip"
    android:orientation="horizontal">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center">
        <TextView
            android:text="@string/main_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="25dip"
            android:textSize="24.5sp"/>
        <Button
            android:id="@+id/continue_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_label"/>
        <Button
            android:id="@+id/new_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_game_label"/>
        <Button
            android:id="@+id/about_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/about_label" />
        <Button 
            android:id="@+id/exit_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit_label"/>
   </LinearLayout>
</LinearLayout>

请帮帮我。
谢谢。

水平布局将元素水平布置,如:

[element][element][element]

垂直面是这样布置的:

[element]
[element]
[element]
因此,在显示xml格式的情况下,它将是一行垂直排列的按钮。。 如果您不需要该布局,那么是的,您可以通过删除一个来简化它

很难在文本中解释,但它看起来是这样的:

----Outer (Horizontal) layout-----
|                                |
|  ---Inner (Vertical) layout-   |
|  |       [Textview]        |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  ---------------------------   |
----------------------------------
文本视图/按钮垂直向下移动

编辑: 实际上,由于外部布局

android:layout_width="fill_parent"
android:layout_height="fill_parent"
它将占据整个屏幕,有30个填充。 然后,内部布局将适合它的内容在垂直方向,并最大限度的宽度水平


我想你可能不需要外部布局。。您可以在一个
线性布局中使用适当的属性获得相同的效果,这里有两个用于保持布局方向的线性布局

第一个或父LinearLayout将覆盖整个屏幕,因为

android:layout_width="fill_parent"
android:layout_height="fill_parent"

背景色

并在队列中水平添加所有内容,因为

android:orientation="vertical"[![enter image description here][1]][1]
android:layout_gravity="center"
第二个线性布局或嵌套布局会将所有内容放在中心,因为

android:orientation="vertical"[![enter image description here][1]][1]
android:layout_gravity="center"
将只占用项目所需的空间,因为

android:layout_height="wrap_content"
android:layout_width="fill_parent"
所有的东西都会按垂直顺序排列,因为

android:orientation="vertical"

谢谢你的回答。为什么要使用布局宽度和布局高度以外的内线布局?那么,如果内部线性布局用于对内部内容(按钮和文本视图)进行排序和组织,为什么要将内部内容(按钮)用于布局?高度和宽度?对不起,我不太清楚你在问什么。。你是在问为什么在布局和内部元素上都有一个高度和宽度的布局吗?高度/宽度将控制单个元素的高度/宽度
LinearLayout
也是一个元素,因此它也可以控制其自身的大小。如果屏幕上有足够的空间显示所有内容,并根据提供的答案,内部布局可用于将按钮和文本视图置于整个布局的中心。内部视图(如按钮和文本视图)的高度和宽度仍将用于拉伸视图(填充父视图)以适合内部线性布局,或在内部线性布局中将视图使用到其最小尺寸(包裹内容)。概述:内部布局用于组织所有内部视图的布局方式,而每个视图仍描述其自身的大小(在其视图组中)。