LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。想知道LinearLayout是如何工作的,首先考虑一下典型的mail.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical" >
<TextView
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="@string/hello" />
</LinearLayout>
在main.xnl中,可以看到,根节点元素是<LinearLayout>,它包含了一个<TextView>元素。这个<LinearLayout>元素控制了它所包含的视图views的顺序。
每一个View和ViewGroup有一些共同的属性,一些例子:
属性描述
layout_width指定View或ViewGroup的宽度
layout_height指定View或ViewGroup的高度
layout_marginTop指定View或ViewGroup上方的额外空间
layout_marginBottom指定View或ViewGroup下方的额外空间
layout_marginLeft指定View或ViewGroup左侧的额外空间
layout_marginRight指定View或ViewGroup右侧的额外空间
layout_gravity指定View或ViewGroup中的子视图的排列位置
layout_weight指定指派给View或ViewGroup的额外空间尺寸
layout_x指定View或ViewGroup的x坐标
layout_y指定View或ViewGroup的y坐标
举个例子,<TextView>元素使用了fill_parent常量,所以的宽度填充了整个它所在布局的宽度。它的高度被设置成wrap_content常量,这意味着,它的高度将和它所在的布局一样。如果不想<TextView>占据整个屏幕,可以把layoutt_width元素设置成wrap_content,就像这样:
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="@string/hello" /><span style="font-family:Arial,Helvetica,sans-serif;">
</span>
www.atcpu.com以上的代码,把TextView的宽度设置成和它所包含的文本的长度一样。
请看如下的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical" >
<TextView
Android:layout_width="100dp"
Android:layout_height="wrap_content"
Android:text="@string/hello" />
<Button
Android:layout_width="160dp"
Android:layout_height="wrap_content"
Android:text="Button" />
</LinearLayout>
这里,就把TextView和Button的宽度设置成了一个固定的值
摘自 manoel的专栏