Android中的四种基本布局

1.LinearLayout

LinearLayout称为线性布局,是一种常用的布局。修改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="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

</LinearLayout>

上述的代码中,指定了一个垂直的线性布局,而且在布局中添加了三个Button,每个Button的长度和宽度都是wrap_content,

其中android:orientation属性为LinearLayout中的控件排列方向,

android:orientation="horizontal" 表示水平方向

android:orientation="vertical" 表示垂直方向

如果不指定android:orientation属性的值,默认的排列方向就是horizontal。

运行程序得到下图的效果:

注意:如果LinearLayout的排列方向是horizontal,内部的控件就绝对不能将宽度指定为match_parent。

如果LinearLayout的排列方向是vertical,内部的控件就绝对不能将高度指定为match_parent。

android:layout_gravity用于指定控件在布局中的对齐方式。在Button中指定android:layout_gravity属性值,代码如下:

<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="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/button3" />

</LinearLayout>

上述的代码就是指定控件在布局中的对齐方式,需要注意的是:

当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的。

当LinearLayout的排列方向是vertical时,只有水平方向上的对齐方式才会生效。

重新运行程序,效果如下图所示:

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="@string/input_message"/>

    <Button
        android:id="@+id/send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/send" />

</LinearLayout>

上述的代码中,在EditText和Button里都将android:layout_weight属性的值指定为1,这表示EditText和Button将在水平方向

平分宽度,重新运行程序,得到下图所示的效果:

为什么将android:layout_weight属性的值同时指定为1就会平分屏幕的?

原因是:系统会先把LinearLayout下所有控件指定的layout_weight相加,得到一个总值,

然后每个控件所占大小的比例就是用该控件的layout_weight值除以刚才算出的总值。

2.RelativeLayout

RelativeLayout又称为相对布局,也是一种常用的布局,修改activity_main.xml中的代码,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/button4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/button5" />

</RelativeLayout>

运行程序,得到如下图的效果:

上面的控件都是相对于父布局进行定位的,那控件相同于控件进行定位呢?修改代码,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/button3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="@string/button4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="@string/button5" />

</RelativeLayout>

注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id。重新运行程序,得到下图的效果:

3.FrameLayout

FrameLayout布局相对来说比较简单,该布局没有任何的定位方式,所有的控件都会摆放在布局的左上角。

4.TableLayout

TableLayout允许使用表格的方式来排列控件,在设计表格时尽量让每一行都拥有相同的列数。

设计一个界面,允许用户输入帐号和密码后登录,修改activity_main.xml中的代码,如下所示:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1" >

    <TableRow>

        <TextView
            android:id="@+id/text_view1"
            android:layout_height="wrap_content"
            android:text="@string/text_view1" />

        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content"
            android:hint="@string/account" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/text_view2"
            android:layout_height="wrap_content"
            android:text="@string/text_view2" />

        <EditText
            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/login"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="@string/login" />
    </TableRow>

</TableLayout>

在TableLayout中每加入一个TableRow就表示在表格中添加一行,然后在TableRow中每加入一个控件,就表示在

该行中加入一列。

注意:TableRow中的控件是不能指定宽度的。

上面的代码中,第三行中使用android:layout_span="2"让登陆按钮占据两列的空间。

因为TableRow中不能指定宽度,就会不能充分利用屏幕,在TableLayout中使用android:stretchColumns="1"属性

可以达到自适应屏幕的目的。

上述的代码中将android:stretchColumns的值指定为1,表示如果表格不能完全占满屏幕宽度,就将第二列进行拉伸。

指定为1,就拉伸第二列;指定为0,就会拉伸第一列。运行代码,会得到下图的效果:

Android学习笔记(九)的更多相关文章

  1. android学习笔记九——RatingBar

    RatingBar==>星级评分条 RatingBar和SeekBar十分相似,它们甚至有相同的父类:AbsSeekBar.两者都允许用户通过拖动来改变进度: 两者最大的区别在于RatingBa ...

  2. Android学习笔记九:Service

    一:Service是什么 Service,服务.一般用于提供需要在后台长期运行的服务(如复杂计算.下载等等耗时任务),其特点是长生命周期的.没有用户界面.在后台运行的. 二:Service的生命周期方 ...

  3. 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...

  4. 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...

  5. 【转】 Pro Android学习笔记(六九):HTTP服务(3):HTTP POST MultiPart

    目录(?)[-] 建立测试环境 开发环境导入第三方JAR HTTP Post Multipart小例子 HTTP POST不仅可以通过键值对传递参数,还可以携带更为复杂的参数,例如文件.HTTP Po ...

  6. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  7. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

  8. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

  9. 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件

    目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...

  10. 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter

    目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...

随机推荐

  1. 在Web大作业——红十字会管理系统里出现的一个Error

    工程描述:根据用户在前端网页的操作对后台数据库进行查询或更新. 错误描述:当对网页进行多次操作后,网页会报错:“数据库超过最大连接数”. 错误分析:每次打开某一网页,都会运行一段JAVA代码连接数据库 ...

  2. MSSQL如何在没有主键的表中删除重复数据

    为了对重复数据进行实验,下面建一个设计不太好(没有主键)表并插入了一些重复数据: create database testdb use testdb ; go create table DupsNoP ...

  3. win32自绘按钮,使用GDI+(一)

    第一次写随笔,我本来想将win32窗口的标题栏设置成渐变色,像这样的效果 但发现找不到设置标题栏属性的api,SetWindowLong也只是增减窗口的固定的样式而已.所以想到一个思路,把标题栏去掉, ...

  4. css性能优化

    1.前端 1.1.减少http请求次数: 1.1.1先了解下HTTP对性能的影响,HTTP是浏览器和服务器通过Interet进行相互通信的协议.HTTP是一种客服端/服务器协议,有请求和响应构成. 浏 ...

  5. 上下文管理、线程池、redis订阅和发布

    一:上下文管理: 对于一些对象在使用之后,需要关闭操作的.比如说:socket.mysql数据库连接.文件句柄等. 都可以用上下文来管理. 语法结构: Typical usage: @contextm ...

  6. The import javax.servlet cannot be resolved

    在STS中,突然把配置的Tomcat删除,换另外一个Tomcat使用时,出现:The import javax.servlet cannot be resolved.这个错误可能是服务器自带的serv ...

  7. 同时有background-size background-positon 两个属性的时候,如何在合并的background样式中展示

    今日写css,遇到background很多属性,于是想合并写,w3c只是说了各个属性都可以合并,但是并没有给出background-size background-positon合并的具体例子 bac ...

  8. windows系统下npm 全局安装路径问题

    安装了nodejs之后,npm的路径默认一直都是appData,表示很讨厌,于是尝试修改在安装目录(D盘空间很大啊) 安装目录:D:\program files\nodejs 一.在nodejs下新建 ...

  9. 怎么使用jquery阻止页面的离开或卸载

    //绑定beforeunload事件$(window).bind('beforeunload',function(){return '您输入的内容尚未保存,确定离开此页面吗?';});//解除绑定,一 ...

  10. 理解闭包 js回收机制

    为什么要有回收机制?why? 打个比方,我有一个内存卡,这个内存是8G的,我把文件,视频,音乐,都保存到了这个内存卡,随着我的储存的内容越来越多,这个内存卡已经保存不了了,如果我还想再把其他的文件保存 ...