1. 什么是Layout?

Layout——界面布局,为应用程序提供界面架构。控制Activity中控件的大小、位置、颜色等属性的方法.

Layout 与 ViewGroup的关系

  • ViewGroup是一个容器,继承自View.
  • ViewGroupLayout和一些其它组件的基类.

在Android中提供了几个常用布局:
LinearLayout 线性布局

RelativeLayout相对布局

FrameLayout 帧布局

AbsoluteLayout绝对布局

TableLayout 表格布局

GridLayout网格布局

今天我们主要讲线性布局,其余的常用布局会在后期文章为大家详细讲述。

2. LinearLayout线性布局:

指子控件以水平或垂直方式排列,正如其名字一样,这个布局中的所有控件在线性方向上依次排列。

常用属性:

  1. android:id:为该组件添加一个资源id,即标识符,可以通过id来找到该布局或者控件。
  2. android:layout_width:布局的宽度,用wrap_content表示组件的实际宽度,match_parent表示填充父容器
  3. android:layout_height:布局的长度,用wrap_content表示组件的实际长度,match_parent表示填充父容器
  4. android:orientation:布局中的排列方式,有两种方式:horizontal水平,vertical竖直,如果不设置则默认水平显示
  5. android:gravity:控制组件所包含的子元素的对齐方式
  6. android:layout_gravity:控制该组件在父容器里的对齐方式
  7. android:background:为该组件添加一个背景图片或者背景颜色,颜色常以六位的十六进制表示
  8. android:layout_margin :外边距,布局或控件距离外部元素的边距
  9. android:layout_padding :内边距,布局或控件距离内部元素的边距
  10. android:layout_weight:权重,除了被显示占据的空间以外的的空间,然后根据权重的大小来分配空间,使用权重通常会把分配该权重方向的宽度设置为0dp,如果未设置0dp,则该控件会占据指定的宽度,然后再加上根据权重来分配的空间

下面依次分别举例说明使用方法

orientation 是一个视图组,可以在一个方向垂直或者水平分布所有子项

android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用.即:left,right,center_horizontal 是生效的.
android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用.即:top,bottom,center_vertical 是生效的.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:orientation="vertical"><TextViewandroid:id="@+id/tv_here"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这里2"android:textColor="@color/black"android:textSize="16sp" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这里2"android:textColor="@color/black"android:textSize="16sp" />
</LinearLayout>

gravity:

android:layout_gravity是本(子)元素相对于父元素的对齐方式设置在子元素上.
android:gravity="bottom|right"是本(父)元素所有子元素的对齐方式,设置在父元素上,多个值用 | 隔开.

其属性值分别为:center(整体居中)、center_vertical(垂直居中)、center_horizontal(水平居中)、right(居右)、left(居左)、bottom(底部)和top(顶部)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@color/color_F9658E"android:gravity="bottom"android:text="这里2"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@color/color_A9BEFF"android:gravity="center_vertical|right"android:text="这里3"android:textColor="@color/black"android:textSize="16sp" />
</LinearLayout>

background

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@mipmap/ic_launcher"android:orientation="vertical"><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:layout_gravity="center_horizontal"android:background="@color/color_FF6F65"android:gravity="center"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" />
</LinearLayout>

padding && margin:
android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上).
android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/color_FF6F65"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:background="@color/color_F9658E"android:gravity="bottom"android:padding="15dp"android:text="这里2"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:background="@color/color_A9BEFF"android:gravity="center_vertical|right"android:paddingRight="15dp"android:text="这里3"android:textColor="@color/black"android:textSize="16sp" />
</LinearLayout>

weight:
android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大.
前提是子元素设置了android:layout_width = "match_parent" 属性 ( 水平方向 )或 android:layout_height = "match_parent"属性( 垂直方向).
如 果 某 个 子 元 素的android:layout_width = "0dp"android:layout_height="0dp" ,则 android:layout_weight 的设置值 对该方向上空间的分配则刚好相反。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:background="@color/color_FF6F65"android:paddingLeft="10dp"android:paddingTop="20dp"android:paddingRight="40dp"android:paddingBottom="60dp"android:text="这里1"android:textColor="@color/black"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_weight="1"android:background="@color/color_F9658E"android:paddingLeft="15dp"android:text="这里2"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_weight="2"android:background="@color/color_A9BEFF"android:paddingRight="15dp"android:text="这里3"android:textColor="@color/black"android:textSize="16sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="0dp"android:layout_height="50dp"android:layout_weight="1"android:background="@color/color_29CFFF"android:paddingLeft="15dp"android:text="这里4"android:textColor="@color/black"android:textSize="16sp" /><TextViewandroid:layout_width="0dp"android:layout_height="50dp"android:layout_weight="2"android:background="@color/color_D6E519"android:paddingRight="15dp"android:text="这里5"android:textColor="@color/black"android:textSize="16sp" /></LinearLayout>
</LinearLayout>

用代码方式编写linearlayout布局

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//创建LinearLayout布局对象LinearLayout liHello = new LinearLayout(this);//对于布局方面的属性这样来设置LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);//设置布局LinearLayout的布局排列方式liHello.setOrientation(LinearLayout.VERTICAL);//设置布局背景颜色liHello.setBackgroundColor(Color.parseColor("#F9658E"));//设置布局内边距,注意这里不可以设置外边距liHello.setPadding(10, 20, 30, 40);//设置组件内所包含的子元素的对齐方式liHello.setGravity(Gravity.CENTER);TextView tvHello = new TextView(this);tvHello.setText("你好");liHello.addView(tvHello, param);//设置显示liHello布局setContentView(liHello);}

结语

我们的软件是由好多个界面组成的,而每个界面又由N多个控件组成,Android中借助布局来让各个空间有条不紊的摆放在界面上。可以把布局看作是一个可以放置很多控件的容器,它可以按照一定的规律调整控件的位置,从而实现精美的界面。布局中也可以放置布局,通过多层布局的嵌套,实现比较复杂的界面。相信小伙伴儿们已经学会LinearLayout的使用方法了,那就赶紧操练起来吧。

PS:如果还有未看懂的小伙伴,欢迎加入我们的QQ技术交流群:892271582,里面有各种大神回答小伙伴们遇到的问题哦~

六大布局之线性布局详解相关推荐

  1. android 图片横竖判断_Android横竖屏切换及其对应布局加载问题详解

    本文为大家分享了Android横竖屏切换及其对应布局加载问题,供大家参考,具体内容如下 第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同 ...

  2. 安卓六大布局之 线性布局(LinearLayout)

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面. Android的六大布局分别是 LinearL ...

  3. Java Swing布局管理器(详解版)

    在使用 Swing 向容器添加组件时,需要考虑组件的位置和大小.如果不使用布局管理器,则需要先在纸上画好各个组件的位置并计算组件间的距离,再向容器中添加.这样虽然能够灵活控制组件的位置,实现却非常麻烦 ...

  4. android五大布局的作用,Android五大布局与实际应用详解

    Android总体有五大布局: 线性布局(LiearLayout): 屏幕垂直或水平方向布局. 帧布局(FrameLayout):控件从屏幕左上角开始布局. 相对布局(RelativeLayout): ...

  5. 判断数组中某个元素除自身外是否和其他数据不同_算法工程师要懂的3种算法数据结构:线性表详解...

    算法思想有很多,业界公认的常用算法思想有8种,分别是枚举.递推.递归.分治.贪心.试探法.动态迭代和模拟.当然8种只是一个大概的划分,是一个"仁者见仁.智者见智"的问题. 其实这些 ...

  6. LDO线性稳压器详解

    一.什么是ldo LDO 是一种线性稳压器.线性稳压器使用在其线性区域内运行的晶体管或 FET,从应用的输入电压中减去超额的电压,产生经过调节的输出电压.所谓压降电压,是指稳压器将输出电压维持在其额定 ...

  7. 【Android】UI布局之线性布局(登录界面代码)

    1.布局管理 组件在activity中呈现的方式,包含组件大小.间距.对齐方式 Android提供了两种布局的实现方式: .在xml配置文件中声明,通过setContentView(R.layout. ...

  8. 线性表详解(静态链表、单链表、双向链表、循环链表)

    目录 申明 1. 线性表的定义 2. 线性表的抽象数据类型 3. 线性表的顺序存储结构 3. 1 顺序存储定义 3. 2 顺序存储方式 3. 3 数据长度与线性表长度区别 3. 4 地址计算方法 4. ...

  9. Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI

    Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI   今天老师的要求是让用LinearLayout布局做自己手机自带的计算器的UI设计,因为io ...

  10. java求线性回归置信区间,线性回归分析详解9:总体回归、置信度、置信区间及其计算方法...

    许栩原创专栏<从入门到高手:线性回归分析详解>第9章:总体回归.置信度.置信区间及其计算方法. 多元回归方程求解后,我们分别确认了回归方程的精度和进行了回归方程的显著性验证,接下来,我们需 ...

最新文章

  1. NetworkManagementService介绍
  2. SVN服务器与客户端的配置
  3. NRF51 BOOTLOADER 编译注意事项
  4. 在bcb中添加activex控件_LinkedCell 属性介绍,OLEObjects 控件
  5. 温网告别了两位美女:伊万和莎娃
  6. 基于JAVA+Servlet+JSP+MYSQL的人事管理系统
  7. iPhone手机投屏小米盒子
  8. C 远程登录linux,远程登录Linux主机进行C编程的操作方法简述.doc
  9. Hibernate与MyBatis的区别
  10. java流程图什么代表活动_举例分析流程图与活动图的区别与联系
  11. 感悟 | 一个 33 岁老程序员的自白:因为那时候我还不懂得…
  12. Linux技术——lsof命令详解
  13. 无形胜有形——0day破解组织探秘
  14. 2018天津大学夏令营机试第二题
  15. ADO简介(未完成)
  16. 统统卸载!再见了,流氓顽固软件!
  17. 拼多多Temu如何批量养国外买家账号进行拉新?
  18. 思维模型 六顶思考帽
  19. 【浙江大学计算机学院】ccnt实验室之,CCNT实验室
  20. 理论+实操:K8S的pod健康检查——live、ready、startup

热门文章

  1. 一般英文(java)
  2. Twister: 迭代MapReduce计算框架
  3. USB Type-C简介
  4. gl.glenable()介绍
  5. Linux各版本内核下载地址
  6. 【数值分析不挂科】第六章 | 线性方程组直接法 迭代法
  7. 台式计算机usb接口无反应6,如何解决电脑的USB接口没反应,教您如何解决
  8. 记一次给博友答疑(shiro相关)
  9. C/C++结构体struct详解
  10. midi java_Java程序中添加播放MIDI音乐功能的实现方法详解