android学习1——LinearLayout
用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的更多相关文章
- Android学习笔记——LinearLayout
该工程的功能是实现LinearLayout 以下的代码是MainActivity.java中的代码 package com.example.linearlayout; import android.a ...
- [Android学习笔记]LinearLayout布局,剩余空间的使用
转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...
- 在LinearLayout中实现列表,列表采用LinearLayout横向布局-android学习
不多讲直接上代码 1.Activity 对应的布局文件如下: <?xml version="1.0" encoding="utf-8"?> < ...
- Android 布局学习之——LinearLayout的layout_weight属性
一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法. 定义 首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- Android学习笔记之LinearLayout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- android系列9.LinearLayout 学习2
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android学习——LinearLayout布局实现居中、左对齐、右对齐
android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...
- Android学习——第一个NDK程序
在前面的学习中,我们已经讲解了关于NDK编程的环境搭建流程,简单的使用我们也通过官网本身自带的例子进行说明了.可是相信大家一定还存在这么的一个疑惑:“如果我要自己利用NDK编写一个Android应用, ...
随机推荐
- 2017 ZSTU寒假排位赛 #2
题目链接:https://vjudge.net/contest/147632#overview. A题,状态压缩一下然后暴力即可. B题,水题,略过. C题,有负数,前缀和不是单调的,因此不能用尺取法 ...
- iOS调试-LLDB学习总结
from:http://www.jianshu.com/p/d6a0a5e39b0e LLDB阐述 LLDB 是一个有着 REPL 的特性和 C++ ,Python 插件的开源调试器.LLDB 绑定在 ...
- IOS开发-ObjC-NSString
NSString是oc语言Foundation框架中常用的类,我根据每个方法的功能将NSString的常用方法分为创建字符串.初始化字符串.判断和比较字符串.大写和小写相互转化.字符串的截取.类型转换 ...
- linux在线预览pdf文件开发思路
准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...
- drag file upload xhr 拖拽异步上传文件
<div id="droptarget" style="width: 500px; height: 200px; background: silver"& ...
- Promise基础
前言: ES2015将Promise引入语言规范,包括fetch等在内的API也构建在Promise之上.作为让js摆脱“回调地狱”的重要一环和众多框架中的重要基础设施之一,学习如何自己实现一个Pro ...
- CocosBuilder 多分辨率基础
最近两个项目大量使用了CocosBuilder, 对于开发效率提高是巨大的. 一直计划写一篇博客谈谈CocosBuilder的多分辨率问题, 懒病加上一些疙疙瘩瘩的小事情, 拖延了一个多月, 才终于下 ...
- EntityFramework 简单入个门
任何一个和数据相关的系统里,数据持久化都是一个不容忽视的问题. 一直以来,Java 平台出了很多 NB 的 ORM 框架,Hibernate.MyBatis等等..NET 平台上,ORM 框架这一块一 ...
- 这个发现是否会是RSA算法的BUG、或者可能存在的破解方式?
笔者从事各种数据加解密算法相关的工作若干年,今天要说的是基于大数分解难题的RSA算法,可能有些啰嗦. 事情的起因是这样的,我最近针对一款芯片进行RSA CRT解密的性能优化.因为期望值是1024bit ...
- 【python基础】之list列表
python提供了一个被称为列表的数据类型,他可以存储一个有序的元素集合. 记住:一个列表可以存储任意大小的数据集合.列表是可变对象,有别于字符串str类,str类是不可变对象. 1.创建一个列表 l ...