这一次咱们来讨论一下LinearLayout这种布局方式。相对来讲,这个布局方式还比较简单。经过设置它的属性android:orientation来决定其包含的View是以水平方向仍是垂直方向摆放。 android
Constant | Value | Description |
---|---|---|
horizontal | 0 | Defines an horizontal widget. |
vertical | 1 | Defines a vertical widget. |
即android:orientation="horizontal"或者android:orientation="vertical"。 布局
若是咱们选择的是水平摆放(horizontal),那么LinearLayout所包含的View会一个挨着一个在一行摆放,全部的View都在一行中,就像下图这样: spa
若是咱们选择的是水平摆放(vertical),那么LinearLayout所包含的View会一个挨着一个竖着摆放,每一个View占一行,就像下图这样: code
从上面两个效果图,能够很清晰看出两种不一样的布局方式差别很大。咱们一共使用的是一个TextView和四个按钮。正常状况下它们应该都显示出来,可是咱们看到水平摆放时,第三个按钮已经挤成一团了,第四个按钮甚至没有显示了。而垂直摆放时,这五个view都显示出来了,每一个View各自单独占一行。 orm
接下来,咱们再为LinearLayout增长一种新的属性:padding。我对它的解释是边缘补白(留空白),就是让LinearLayout中的内容距离边缘留出必定的空白,这实际上是为了美观,好看。如今让咱们来看看实际效果,就以上面的五个View为例吧,给LinearLayout增长下面两个属性值: xml
android:paddingLeft="16dp" android:paddingRight="16dp"
运行效果以下图所示: ip
能够很明显的看出来,在这些View的左边出现了一些空白,是否是? utf-8
下面,咱们来看官网上的一个例子: get
<?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:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /> </LinearLayout>
咱们先看看它的运行效果是什么样子的: string
这个界面有点相似于电子邮件的书写界面。 咱们细看一下那个XML布局文件的内容。
<LinearLayout>元素的几个属性咱们在前面的介绍中,基本上已经扫清了。除了那个fill_parent,暂时我尚未分清它与match_parent的区别,不过这个并不影响咱们看其它的View。
在<LinearLayout>中有三个<EditText>和一个<Button>。三个<EditText>中都有两个咱们已经熟悉的属性,那就是androdi:layout_width和android:layout_height。而它们的取值出现了一些新的变化。
首先,fill_parent,咱们能够理解为LinearLayout容许有多宽,我就扩展为多宽。