本文简述在Android开发中布局的简单应用,属于基础知识,仅供学习分享使用。

概述

在Android UI开发中,布局类型主要有两种:LinearLayout(线性布局)和RelativeLayout(相对布局),两种布局类型各有各的优势与使用场景。

LinearLayout(线性布局)

线性布局允许所有的子元素,以单独的方向进行排列(水平或垂直),所有的元素像栈一样一个接一个的插入,所以如果是垂直(vertical)方向,则每一行只有一个元素。如果是水平( horizontal)方向,则只有一行。(如下图1所示)

线性布局重要属性

android:orientation 设置排列的方向。主要有两个值:horizontal(水平),vertical(垂直)。

android:layout_weight 权重,按比例分配剩余空间。

 

        (图1)                                                       (图2)

RelativeLayout(相对布局)

相对布局是指所有子元素以相对的位置进行定位。一个元素可以通过相对于指定的同级元素(如,左边,右边,上边,下边)进行定位,也可以通过父元素进行定位(如,布局控件的顶端,左端,右端,底部等)(如上图2 所示)。如果发现页面中有多个线性布局进行嵌套,那么你就应该用一个相对布局来替换它。

相对布局重要属性

  • android:layout_alignParentTop 是否位于父控件的顶部(true 或 false)
  • android:layout_alignParentBottom 是否位于父控件的底部(true 或 false)
  • android:layout_alignParentLeft 是否位于父控件的左边(true 或 false)
  • android:layout_alignParentRight 是否位于父控件的右边(true 或 false)
  • android:layout_centerInParent 是否位于父控件的中心(true 或 false)
  • android:layout_toLeftOf 位于指定控件的左边(值为控件的ID)
  • android:layout_toRightOf 位于指定控件的右边(值为控件的ID)
  • android:layout_above 位于指定控件的上边(值为控件的ID)
  • android:layout_below 位于指定控件的下边(值为控件的ID)

实例截图

如下图1所示为线性布局(相对简单),如下图2所示,为相对布局(相对复杂)

 

图3                                                                         图4

布局源程序

线性布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.hex.demolayout.LinearActivity">
<TextView
android:id="@+id/tv_text"
android:text="@string/tv_name"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/txt_name"
android:hint="@string/hint_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_age"
android:text="@string/tv_age"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/txt_age"
android:hint="@string/hint_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_sex"
android:text="@string/tv_sex"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/rg_sex"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_male"
android:text="@string/male"
android:textSize="20sp"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/rb_female"
android:textSize="20sp"
android:text="@string/female"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<TextView
android:id="@+id/tv_love"
android:text="@string/love"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/ck_basketball"
android:text="@string/basketball"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/ck_football"
android:text="@string/football"
android:textSize="20dp"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/ck_game"
android:text="@string/game"
android:textSize="20dp"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/tv_school"
android:text="@string/school"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/txt_school"
android:hint="@string/hint_school"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_addr"
android:text="@string/addr"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/txt_addr"
android:hint="@string/hint_addr"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bn_submit"
android:text="@string/bn_submit"
android:textColor="@color/colorBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

相对布局

 <?xml version="1.0" encoding="utf-8"?>
<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="com.hex.demolayout.MainActivity"> <TextView
android:id="@+id/tv_title"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="@color/colorBlue"
android:layout_margin="3dp"
android:text="@string/nine_tip"/>
<TextView
android:id="@+id/tv_center"
android:text="@string/center"
android:textSize="30dp"
android:layout_margin="3dp"
android:onClick="open"
android:textColor="@color/colorRed"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_east"
android:text="@string/east"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_west"
android:text="@string/west"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_north"
android:text="@string/north"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_south"
android:text="@string/south"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_below="@id/tv_title"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_east_south"
android:text="@string/east_south"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_below="@id/tv_title"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_west_south"
android:text="@string/west_south"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_below="@id/tv_title"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_east_north"
android:text="@string/east_north"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_west_north"
android:text="@string/west_north"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorRed"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_xun"
android:text="@string/xun"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_below="@id/tv_east_south"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_li"
android:text="@string/li"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_below="@id/tv_south"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_kun"
android:text="@string/kun"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_below="@id/tv_west_south"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_zen"
android:text="@string/zen"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_toRightOf="@id/tv_east"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_dui"
android:text="@string/dui"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_toLeftOf="@id/tv_west"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_gen"
android:text="@string/gen"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_above="@id/tv_east_north"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_kan"
android:text="@string/kan"
android:textSize="30dp"
android:layout_margin="3dp"
android:layout_above="@id/tv_north"
android:textColor="@color/colorGreen"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_qan"
android:text="@string/qan"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorGreen"
android:layout_above="@id/tv_west_north"
android:layout_alignRight="@id/tv_west_north"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_mu"
android:text="@string/mu"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_below="@id/tv_xun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_huo"
android:text="@string/huo"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_below="@id/tv_li"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_tu"
android:text="@string/tu"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_below="@id/tv_kun"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_mu2"
android:text="@string/mu"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_below="@id/tv_zen"
android:layout_alignLeft="@id/tv_zen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_tu2"
android:text="@string/tu"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_below="@id/tv_center"
android:layout_alignLeft="@id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_jin"
android:text="@string/jin"
android:textSize="30dp"
android:textColor="@color/colorBlue"
android:layout_margin="3dp"
android:layout_below="@id/tv_dui"
android:layout_alignLeft="@id/tv_dui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_tu3"
android:text="@string/tu"
android:textSize="30dp"
android:layout_margin="3dp"
android:textColor="@color/colorBlue"
android:layout_above="@id/tv_gen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_shui"
android:text="@string/shui"
android:textSize="30dp"
android:layout_margin="3dp"
android:layout_above="@id/tv_kan"
android:textColor="@color/colorBlue"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_jin2"
android:text="@string/jin"
android:textSize="30dp"
android:layout_margin="3dp"
android:layout_above="@id/tv_qan"
android:textColor="@color/colorBlue"
android:layout_alignLeft="@id/tv_qan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

备注

基础知识学习,从零开始。

一起学Android之Layout的更多相关文章

  1. 从零开始学android开发- layout属性介绍

    android:id 为控件指定相应的ID android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:gravity 指定Vi ...

  2. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  3. 学Android开发 这19个开发工具助你顺风顺水

    学Android开发 这19个开发工具助你顺风顺水 要想快速开发一个Android应用,通常会用到很多工具,巧妙利用这些工具,能让我们的开发工作事半功倍,节省大量时间,下面大连Android开发培训小 ...

  4. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

  5. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

  6. 一步一步学android控件(之六) —— MultiAutoCompleteTextView

    今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...

  7. CSharp程序员学Android开发---3.Android内部元素不填充BUG

    最近公司组织项目组成员开发一个Android项目的Demo,之前没有人有Andoid方面的开发经验,都是开发C#的. 虽说项目要求并不是很高,但是对于没有这方面经验的人来说,第一步是最困难的. 项目历 ...

  8. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  9. Android开发学习之路-该怎么学Android(Service和Activity通信为例)

    在大部分地方,比如书本或者学校和培训机构,教学Android的方式都基本类似,就是告诉先上原理方法,然后对着代码讲一下. 但是,这往往不是一个很好的方法,为什么? ① 学生要掌握这个方法的用途,只能通 ...

随机推荐

  1. 从css 3d说到空间坐标轴

    有一次我们说到掷骰子那个游戏,当时是用了一个steps属性+雪碧图来制作帧动画,这当然颇为不错,但其实一开始我想的不是这样的,我想的是用真的3d和动画去做,这个方案涉及到不少空间的知识,今天来给大伙好 ...

  2. Java消息系统简单设计与实现

    前言:由于导师在我的毕设项目里加了消息系统(本来想水水就过的..),没办法...来稍微研究研究吧..简单简单... 需求分析 我的毕设是一个博客系统,类似于简书这样的,所以消息系统也类似,在用户的消息 ...

  3. Hibernate学习——持久化类的学习

    A.概念 持久化:将内存中的对象持久化(存储)到数据库的过程.Hibernate就是持久化的框架. 持久化类:一个普通java对象与数据库的表建立了映射关系,那么这个类在Hiberna中被称为持久化类 ...

  4. MySQL找不到msvcp140.dll”

    没有安装VC++2015(Microsoft Visual C++ 2015 Redistributable), 下载地址 点击download,一个64位的一个32位的.

  5. Windows Server 2016-PS筛选导出用户邮箱属性包含某字段列表

    生产环境中我们往往会遇到以多个邮箱别名结尾的情况,如何快速导出当前域用户邮箱以某字段或后缀结尾的用户列表信息变得尤为重要,本例简单汇总下如何通过Powershell快速筛选出当前邮箱信息包含azure ...

  6. 关于int main( int argc, char* argv[] ) 中arg和argv参数的解析及调试

    https://blog.csdn.net/LYJ_viviani/article/details/51873961 https://stackoverflow.com/questions/30241 ...

  7. Vue2.x源码学习笔记-Vue源码调试

    如果我们不用单文件组件开发,一般直接<script src="dist/vue.js">引入开发版vue.js这种情况下debug也是很方便的,只不过vue.js文件代 ...

  8. 年末展望:Oracle 对 JDK收费和.NET Core 给我们的机遇

    2018年就结束了,马上就要迎来2019年,这一年很不平凡,中美贸易战还在继续,IT互联网发生急剧变化,大量互联网公司开始裁员,微软的市值在不断上升 ,在互联网公司的市值下跌过程中爬到了第一的位置,我 ...

  9. 从Android源码修改cpu信息

    cpuinfo 网上的文章都是怎么查看/proc/cpuinfo,一直以为这种东西没法改呢,我还是太天真了./proc/cpuinfo是个文件,只读,想直接写肯定不行的.今天研究了一下,发现它的输出逻 ...

  10. GitHub的Repository权限将public转为private

    2019年1月7日,GitHub CEO Nat Friedman 于官方博客公开发文,称“New year, new GitHub”,宣布从此将免费无限地为普通用户提供私有仓库服务. 因此,我们可以 ...