用linearLayout,上面放4个按钮,不作任何设置。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"
>
<Button android:id="@+id/bn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn1"
/>
<Button android:id="@+id/bn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn2"
/>
<Button android:id="@+id/bn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn3"
/>
<Button android:id="@+id/bn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn4"
/>
<Button android:id="@+id/bn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn5"
/>
</LinearLayout>

显示的效果如下图下所示:



从上图可以看出,xml中一共是5个按钮,但是linearlayout默认是横向排列,所以第5个按钮已经看不到了.下面加一个配制选项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:orientation指定是横向排列(horizontal)还是纵向(vertical)排列.默认是横向,设置成vertical效果如下:



android:gravity控制竖直方向的对齐方式,看下面的例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
>

指定竖直方向为底部对齐,效果如下图所示



android:gravity的参数还可以组合,比如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom|center"
>

效果是下方居中对齐,不再帖图片了.

源代码地址:https://github.com/zhouyang209117/AndroidTutorial/tree/master/Crazy/ch2/LinearLayout

看布局管理器嵌套的例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"/>
</LinearLayout>
</LinearLayout>

按钮3,4被看成一个整体,在这个整体中Linearlayout的排列规则和上面讲到的相同.排列效果如下:



有一个问题:android:layout_width="fill_parent"表示填充父容器的宽度.但LinearLayout是根布局管理器,那么它填充的是谁的?

android学习1——LinearLayout的更多相关文章

  1. Android学习笔记——LinearLayout

    该工程的功能是实现LinearLayout 以下的代码是MainActivity.java中的代码 package com.example.linearlayout; import android.a ...

  2. [Android学习笔记]LinearLayout布局,剩余空间的使用

    转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...

  3. 在LinearLayout中实现列表,列表采用LinearLayout横向布局-android学习

    不多讲直接上代码 1.Activity 对应的布局文件如下: <?xml version="1.0" encoding="utf-8"?> < ...

  4. Android 布局学习之——LinearLayout的layout_weight属性

    一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法.     定义     首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...

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

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

  6. Android学习笔记之LinearLayout

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. android系列9.LinearLayout 学习2

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. Android学习——LinearLayout布局实现居中、左对齐、右对齐

    android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...

  9. Android学习——第一个NDK程序

    在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...

随机推荐

  1. RabbitMQ消息队列(九):Publisher的消息确认机制

    在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...

  2. HDU 2830:Matrix Swapping II(思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=2830 题意:-- 思路:对于每一列,它是固定的,用dp[][]处理出连续的长度.例如: 假设我们扫第四列的时候, ...

  3. SQL Select结果增加自增自段(网转)

    http://www.cnblogs.com/haver/archive/2011/07/14/2106349.html/* 方法一*/ SELECT 序号= (SELECT COUNT(客户编号) ...

  4. 3.IP协议,ARP协议,RARP协议

    1.IP协议 IP协议是TCP/IP协议的核心,所有的TCP,UDP,IMCP,IGCP的数据都以IP数据格式传输.要注意的是,IP不是可靠的协议,这是说,IP协议没有提供一种数据未传达以后的处理机制 ...

  5. iOS 之 通知

    步骤一,注册消息: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserProfileSu ...

  6. Angular - - $anchorScroll、$controller、$document

    $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到相应的元素. 监听$location.hash()并且滚动到url指定的锚点的地方.可以通过 ...

  7. 强行在MFC窗体中渲染Cocos2d-x 3.6

    [前言] 把Cocos2dx渲染到另一个应用程序框架中的方法,在2.x时代有很多大神已经实现了,而3.x的做法网上几乎找不着.这两天抽空强行折腾了一下,不敢独享,贴出来供大家参考. [已知存在的问题] ...

  8. 基于监听的事件处理——Activity本身作为事件监听器

    这种形式使用Activity本身作为监听器类,可以直接在Activity类中定义事件处理方法,这种形式非常简洁.但这种做法有两个缺点: 这种形式可能造成程序结构混乱,Activity的主要职责应该是完 ...

  9. Servlet的请求HttpServletRequest

    一.从容器到HttpServlet 1.web容器作了什么 web容器做的事情就是,创建Servlet实例,并完成Servlet的名称注册及URL模式的对应.在请求来到时,web容器会转发给正确的Se ...

  10. session的一些方法

    session的一些方法: package com.stono.servlet.listenerorder; import java.io.IOException; import java.io.Pr ...