Android学习笔记_3_四种布局
Android布局是应用界面开发的重要一环,在Android中,共有四种布局方式,
分别是:FrameLayout( 帧布局 )、LinearLayout (线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)。
1.LinearLayout
线性布局LinearLayout 在单一方向上对齐所有的子视图-竖向或者横向,这依赖于你怎么定义方向orientation 属性。所有子视图依次堆积,所以一个竖向列表每行只有一个子视图,不管它们有多宽,而一个横向列表将只有一行高(最高子视图的高度,加上填充)。一个线性布局LinearLayout 会考虑子视图之间的边缘空白margins以及每个子视图的引力属性(靠右,居中,或者靠左)。
2.RelativeLayout
相对布局RelativeLayout允许子视图指定它们和父视图或彼此之间的相对位置(通过ID指定)。因此你可以按正确的顺序对齐两个元素,或者让一个视图在另外一个下面,居于屏幕中间,左边的中间,等等。元素通过给定顺序来绘制,因此如果这第一个元素在屏幕中间,其他以它对齐的元素都会对齐到屏幕中间。同样,因为这个顺序,如果使用XML来指定这个布局,你将引用的元素(为了定位其它视图对象)必须被列在XML文件中,在你通过引用ID从其他视图中引用它之前。
其中一些特性直接由元素支持,另外一些由它的LayoutParams成员变量支持(为所有这个屏幕中的元素子类化RelativeLayout,因为所有元素都是RelativeLayout父对象的子元素)。已定义的相对布局RelativeLayout参数是:width,height,below,alignTop,toLeft,padding[Bottom|Left|Right|Top],以及 margin[Bottom|Left|Right|Top]。注意其中一些参数明确支持相对布局位置-它们的数值必须是你的相对位置元素的ID。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvPhone"
android:id="@+id/tv1" /> <EditText
android:id="@+id/editPh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv1"
android:layout_below="@+id/tv1"
android:hint="@string/phoneMsg"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editPh"
android:layout_below="@+id/editPh"
android:text="@string/tvSMS" />
<EditText
android:id="@+id/etSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etSMS"
android:layout_below="@+id/etSMS"
android:text="@string/send" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/etSMS"
android:text="Button" /> </RelativeLayout>
3.TableLayout
表格布局TableLayout把它的子视图定位到行和列中。表格布局容器不显示行,列和单元的边界线。表的列和最多行单元数一样多。一个表可以有空单元,但是单元不能像HTML里面那样跨列。
TableRow 对象是一个TableLayout的子视图(每个TableRow定义了表中的一个单独行)。每行有0或多个单元,可用任何其他视图定义。因此,行单元可能由各个视图对象组成,如ImageView或TextView对象。一个单元也可以是一个ViewGroup对象(比如,你可以嵌入另一个表布局作为一个单元)。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="60dp"
android:gravity="center_vertical"
android:text="@string/tvPhone"
android:id="@+id/tv1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvPhone"
android:id="@+id/tv2" />
</TableRow>
</TableLayout>
4.FrameLayout
FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/movie"
android:contentDescription="@string/movie"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/paly"
android:layout_gravity="center"
android:contentDescription="@string/play"/>
</FrameLayout>
android:gravity设置空间中内容的对齐方式,android:layout_gravity设置控件本身的对齐方式
当android:gravity和android:padding同时做用时,先按gravity进行排布,再按padding调整
android:padding指定内边距,android:layout_margin指定外边距
Android学习笔记_3_四种布局的更多相关文章
- 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版
目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...
- 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果
目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...
- 【转】Pro Android学习笔记(四):了解Android资源(下)
处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...
- Android学习笔记(九)——布局和控件的自定义
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...
- Android学习笔记(四)深入探讨Activity
在应用程序中至少包含一个用来处理应用程序的主UI功能的主界面屏幕.这个主界面一般由多个Fragment组成,并由一组次要Activity支持.要在屏幕之间切换,就必须要启动一个新的Activity.一 ...
- Android学习笔记(四)——再探Intent
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们可以使用 Intent 来启动一个活动, 还可以在启动活动的时候传递数据的,下面一起来看一下: 一.向下一 ...
- Android学习笔记六:六大布局
六大界面布局方式包括: 线性布局(LinearLayout).帧布局(FrameLayout).表格布局(TableLayout).相对布局(RelativeLayout).绝对布局(Absolute ...
- 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区
目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...
- 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考
目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...
随机推荐
- 防盗链与springboot代理模式(图片文件转发)
在搭建自己的博客网站的时候,很有可能要引入一些外部图片,毕竟多数人最开始不是在自己的平台上写博客. 因某种需要,搬运自己以前写的博客到自己的网站时,在图片这一步可能会出现问题,无法显示.其中往往就是防 ...
- [Modelsim] 仿真的基本操作
切换路径,建立库并编译所有源文件之后, 键入命令: vopt +acc topmodulename -o top vsim top 其中topmodulename是顶层模块的名称.
- Pygame 加载音频
Python Learning:Pygame 加载音频 Python 中自带的 winsound 模块 winsound 模块中 Beep 方法可以调用系统的蜂鸣器,接受一个为 frequency 的 ...
- C#学习笔记4
1.C#只支持单一继承,若想要实现多重继承的效果.有2种方式: 第一种为传递继承,A为基类,B继承A,C继承B,通过传递来增迭要包含的元素,但这种继承的设计,在业务上具有明显的从属关系. 第二种为聚合 ...
- tushare获取的数据与mysql数据库交互简单范例
#!/usr/bin/python2.7# -*- coding: UTF-8 -*- import tushare as tsimport pandas as pdfrom sqlalchemy i ...
- 如何修改Vue打包后文件的接口地址配置
1.背景 常规的vue项目分为本地环境和生产环境,我们只要对 config 文件夹下的 dev.env.js 和 prod.env.js 做相应的配置即可.但是最近在做的项目中,涉及到私有化部署,就是 ...
- Easy deployment
Use simple ssh and shell scripts to deploy, upgrade, rollback and reconfigure linux servers. https:/ ...
- qt中的lineEdit文本输入框的输入类型限制(三种验证类)
qt的三种验证类: 1.输入int类型 QValidator *validator=new QIntValidator(100,999,this): QLineEdit *edit=new QLine ...
- js 时间特效
http://example.com:1234/test.htm#part2:Hash的作用. http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
- GridView——标题行自适应单元格列宽与选中单元格变色
首先看效果图: 主要实现—— 1.前台GridView代码: <asp:GridView Height="100%" Width="98%" ID=&qu ...