本篇博文最后修改时间:2016年7月8日,22:16
一、简介
本篇介绍基本布局——LinearLayout。
二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、基本布局——LinearLayout
1、布局的作用
一个丰富的界面总是要由很多个控件组成的,
那我们如何才能让各个控件都有条不紊地摆放在界面上,而不是乱糟糟的呢?
这就需要借助布局来实现了。
布局是一种可用于放置很多控件的容器,
它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。
当然,布局的内部除了放置控件外,也可以放置布局,
通过多层布局的嵌套,我们就能够完成一些比较复杂的界面实。
2、基本布局——LinearLayout
LinearLayout 又称作线性布局,是一种非常常用的布局。
正如它名字所描述的一样,这个布局会将它所包含的控件在线性方向上依次排列。
相信你之前也已经注意到了,我们在上一节中学习控件用法时,
所有的控件就都是放在LinearLayout 布局里的,
因此上一节中的控件也确实是在垂直方向上线性排列的。
既然是线性排列,肯定就不仅只有一个方向,
那为什么上一节中的控件都是在垂直方向排列的呢?
这是由于我们通过android:orientation 属性指定了排列方向是vertical,
如果指定的是horizontal,控件就会在水平方向上排列了。
下面我们通过实战来体会一下,修改
activity_main.xml 中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
我们在LinearLayout 中添加了三个Button,每个Button 的长和宽都是wrap_content,
并指定了排列方向是vertical。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
将android:orientation 属性的值改成了horizontal,
这就意味着要让LinearLayout 中的控件在水平方向上依次排列,
当然如果不指定android:orientation 属性的值,
默认的排列方向就是horizontal。
这里需要注意,如果LinearLayout 的排列方向是horizontal,
内部的控件就绝对不能将宽度指定为match_parent,
因为这样的话单独一个控件就会将整个水平方向占满,
其他的控件就没有可放置的位置了。
同样的道理,如果LinearLayout 的排列方向是vertical,
内部的控件就不能将高度指定为match_parent。
3、LinearLayout 的关键属性用法android:layout_gravity 属性
它和我们上一节中学到的android:gravity 属性看起来有些相似,
这两个属性有什么区别呢?其实从名字上就可以看出,
android:gravity 是用于指定文字在控件中的对齐方式,
android:layout_gravity 是用于指定控件在布局中的对齐方式。
android:layout_gravity 的可选值和android:gravity 差不多,
但是需要注意, 当LinearLayout 的排列方向是horizontal 时,
只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,
每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。
同样的道理,当LinearLayout 的排列方向是vertical 时,
只有水平方向上的对齐方式才会生效。
修改activity_main.xml 中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Button 3" />
</LinearLayout>
由于目前LinearLayout 的排列方向是horizontal,
因此我们只能指定垂直方向上的排列方向,
将第一个Button 的对齐方式指定为top,
第二个Button 的对齐方式指定为center_vertical,
第三个Button 的对齐方式指定为bottom。
4、LinearLayout 中的另一个重要属性android:layout_weight
这个属性允许我们使用比例的方式来指定控件的大小,
它在手机屏幕的适配性方面可以起到非常重要的作用。
比如我们正在编写一个消息发送界面,需要一个文本编辑框和一个发送按钮,
修改activity_main.xml 中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something"
/>
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send"
/>
</LinearLayout>
你会发现,这里竟然将EditText 和Button 的宽度都指定成了0,
这样文本编辑框和按钮还能显示出来吗?
不用担心,由于我们使用了android:layout_weight 属性,
此时控件的宽度就不应该再由android:layout_width 来决定,
这里指定成0 是一种比较规范的写法。
然后我们在EditText 和Button 里都将android:layout_weight 属性的值指定为1,
这表示EditText 和Button 将在水平方向平分宽度。
为什么将android:layout_weight 属性的值同时指定为1 就会平分屏幕宽度呢?
其实原理也很简单,系统会先把LinearLayout 下所有控件指定的layout_weight 值相加,得到一个总值,
然后每个控件所占大小的比例就是用该控件的layout_weight 值除以刚才算出的总值。
因此如果想让EditText 占据屏幕宽度的3/5,Button 占据屏幕宽度的2/5,
只需要将EditText 的layout_weight 改成3,Button 的layout_weight 改成2就可以了。
我们还可以通过指定部分控件的layout_weight 值, 来实现更好的效果。
修改activity_main.xml 中的代码,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something"
/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>
</LinearLayout>
这里我们仅指定了EditText 的android:layout_weight 属性,并将Button 的宽度改回wrap_content。
这表示Button 的宽度仍然按照wrap_content 来计算,而EditText 则会占满屏幕所有的剩余空间。
使用这种方式编写的界面,不仅在各种屏幕的适配方面会非常好,而且看起来也更加舒服。