本文介绍 Android 界面开发中最基本的四种布局LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法及这四种布局中常用的属性。

LinearLayout 线性布局,布局中空间呈线性排列

RelativeLayout 相对布局,通过相对定位的方式,控制控件位置

FrameLayout 帧布局,最简单的布局,所有控件放置左上角

TableLayout 表格布局,以行列方式控制控件位置

四种布局示例

1.LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="150dp"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="垂直1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="垂直2" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平2" />

android:layout_width="match_parent"

android:layout_height="150dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="top"

android:text="水平上对齐" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:text="水平垂直居中" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:text="水平下对齐" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="3"

android:hint="请输入..."/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="提交" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:hint="请输入..."/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="提交" />

orientation:horizontal(水平)/vertical(垂直),表示线性排列的方向。

layout_width/layout_height:元素的宽度与高度

layout_gravity:top/bottom/center/left/right/etc,表示当前元素相对父元素的对齐方式,多种对齐方式用“|”隔开,右上对齐:top|right。

layout_weight:占据空间的比例,例如元素A和B,A设置为1,B设置为3, 元素A、B分别占空间的1/4、3/4,此时元素宽度不由layout_width决定,设置为0dp是比较规范的写法。

layout_weight 若元素A设置为1,元素B不设置,将layout_width设置为具体的值或wrap_content,那么元素B的宽度由layout_width决定,元素A将占满屏幕剩下的空间。

2.RelativeLayout

android:layout_width="match_parent"

android:layout_height="300dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentBottom="true"

android:text="我在左下"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="我在中间"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="我在右上"/>

android:layout_width="match_parent"

android:layout_height="300dp">

android:id="@+id/button_2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="参照按钮"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button_2"

android:layout_toRightOf="@id/button_2"

android:text="我在右上"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button_2"

android:layout_toLeftOf="@id/button_2"

android:text="我在左下"/>

以下属性值为true/false

layout_centerHorizontal/layout_centerVertical: 水平居中、垂直居中

layout_centerInparent: 相对父元素垂直&水平居中

layout_alignParentBottom: 元素下边界和父元素下边界对齐

layout_alignParentLeft: 左边界对齐

layout_alignParentRight: 右边界对齐

layout_alignParentTop: 上边界对齐

以下属性值为控件id

layout_above/layout_below: 在某元素的上方/下方

layout_toLeftOf/layout_toRightOf: 在某元素的左方/右方

layout_alignTop/layout_alignBottom: 元素上(下)边界与某元素上(下)边界对齐

layout_alignLeft/layout_alignRight: 左(右)边界对齐

3.FrameLayout

所有元素都放置在布局的左上角

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是一个按钮"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是一个输入框"/>

4.TableLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_height="wrap_content"

android:text="邮箱"/>

android:layout_height="wrap_content"

android:inputType="textEmailAddress"

android:hint="请输入您的邮箱" />

android:layout_height="wrap_content"

android:text="密码"/>

android:layout_height="wrap_content"

android:inputType="textPassword"

android:hint="请输入密码" />

android:layout_height="wrap_content"

android:layout_span="2"

android:text="注册" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:stretchColumns="1">

...

TableRow:代表表格布局的一行,行内一个元素代表一列。

layout_span:合并单元格,设置为2,代表该元素占据2列空间。

stretchColumns: TableRow中无法指定空间宽度,那么需要用到该属性,设置为1,表示拉伸第2列(0为第1列)与屏幕一样宽,效果如TableLayout的第二张图。

5.自定义布局

Android中,布局下可以放置控件,也可以放置子布局。如果子布局内容较为独立且经常使用,例如标题栏,或者布局比较复杂,这时候可以考虑使用自定义布局的形式导入。方法很简单。

新建一个布局文件,例如example.xml

在父布局中引入:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

以上就是Android最基本的四种布局的详细内容介绍,希望对大家的学习有所帮助。

android 自定义relativelayout,Android布局(RelativeLayout、TableLayout等)使用方法相关推荐

  1. 自定义背景android,Android自定义Button并设置不同背景图片的方法

    本文实例讲述了Android自定义Button并设置不同背景图片的方法.分享给大家供大家参考,具体如下: 1.自定义MyButton类 public class MyButton extends Bu ...

  2. 精通Android自定义View(十六)invalidate方法和requestLayout方法

    1 简述 requestLayout方法会导致View的onMeasure.onLayout.onDraw方法被调用:invalidate方法则只会导致View的onDraw方法被调用 2 reque ...

  3. .Net程序猿玩转Android开发---(7)相对布局RelativeLayout

                 相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行 ...

  4. android 自定义event,Android运用onTouchEvent自定义滑动布局

    写在自定义之前 我们也许会遇到,自定义控件的触屏事件处理,先来了解一下View类中的,onTouch事件和onTouchEvent事件. 1.boolean onTouch(View v, Motio ...

  5. Android自定义menu菜单布局

    一:先写一个自定义的菜单布局 menu_gallery.xml: <?xml version="1.0" encoding="utf-8"?> &l ...

  6. android自定义radiogroup,Android 自定义View实现任意布局的RadioGroup效果

    前言 RadioGroup是继承LinearLayout,只支持横向或者竖向两种布局.所以在某些情况,比如多行多列布局,RadioGroup就并不适用 . 本篇文章通过继承RelativeLayout ...

  7. zxing android 自定义,ZXing Android Embedded 的使用以及自定义布局 · 小憩之地

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 想做个扫描条形码的功能,第一想到的就是 ZXing 了.ZXing 的功能很强大,通常不需要完整把整个库都作为依赖放进应 ...

  8. android自定义dialog布局文件,Android自定义Dialog及其布局

    实际项目开发中默认的Dialog无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中,才能在 ...

  9. android 自定义推送布局_Notification的基本用法以及使用RemoteView实现自定义布局

    Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) ...

  10. android自定义视图添加布局文件,android – 添加自定义视图作为XML布局的视图

    方案如下: 我有一个活动RunTrainingWorkoutsView,它使用XML布局_run_workout.xml_,其中一些标签由CountDownTimer更新.工作正常- 现在,除了每秒通 ...

最新文章

  1. Android分享功能
  2. python3 计算时间间隔
  3. php获取汉字的长度,php中处理汉字字符串长度:strlen和mb_strlen
  4. 数字图像处理基础与应用 第四章
  5. 8、Linux基础(四)
  6. 格式化输出,while循环,运算符,初识编码
  7. 求一个二维数组的最大子矩阵的和
  8. Linux - Ubuntu下JDK配置
  9. Aizu - 2249
  10. 计算1至n中数字X出现的次数
  11. GitHub客户端使用
  12. 进程调度算法-先来先服务、最短作业优先调度算法和高响应比优先调度算法
  13. 要给新家搭建智能家居,到底该从哪里入手?回复被「坑惨」的业主
  14. python求幂_python 求幂
  15. 【商品架构day6】阿里闲鱼亿级商品结构化 - 用技术的力量去降低商户录入成本
  16. 我个人关于2019年双十一平台的使用
  17. 打印机不能正常打印怎么办
  18. 合成文本图像_设计中哪个更重要:图像还是文本?
  19. 微信模版消息发送失败
  20. 透明小时钟开发流程与总结

热门文章

  1. 20新闻与传播考研视频资料持续更新中
  2. 【Android】将图片转为xml文件
  3. 关于zotero如何在另一台电脑上继续同步工作的问题
  4. 访《VR开发实战》作者冀盼——VR 开发者的核心竞争力
  5. [VSHalcon] 无法定位程序输入点于动态链接库 .dll上
  6. 单晶XRD培养技术(二)
  7. python语言中内置的字符串排版方法_python基础知识(python内置数据类型-字符串)...
  8. myeclipse trial expired
  9. 数据结构中的status
  10. 解决TeamViewer可免费用于个人用途,但所使用的设备数量受限。您已达到设备数量上限