我在Hello Android书中读到了这段代码,我不知道为什么要编写两个LinearLayout。 我可以将其中一个删除或移动到其他? 为什么我们使用嵌套的LinearLayout?它做了什么?
<?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>
请帮帮我。 谢谢。
答案 0 :(得分:4)
水平布局横向布局元素:
[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
答案 1 :(得分:0)
这里2 LinearLayout用于维护布局方向
由于
,第一个或父级LinearLayout将设法覆盖整个屏幕android:layout_width="fill_parent"
android:layout_height="fill_parent"
使用
背景色
并且因为
而在队列中水平添加所有内容android:orientation="vertical"[![enter image description here][1]][1]
第二个LinearLayout或嵌套的一个会因为
而将所有内容放在中心位置android:layout_gravity="center"
由于,将只占用项目所需的空格
android:layout_height="wrap_content"
android:layout_width="fill_parent"
,由于
,所有内容都将按垂直顺序排列android:orientation="vertical"