Android 学习笔记-布局
来源:互联网 发布:淘宝网情侣装dongji 编辑:程序博客网 时间:2024/05/20 06:40
前言
布局就是把界面中的控件按照某种规律摆放在指定位置,主要是为了解决应用程序在不同手机中的显示问题。
在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来了解下Android开发中最常用的五大布局介绍和相关属性的设置。
概述
Android中常用的5大布局方式有以下几种:
线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
帧布局(FrameLayout):组件从屏幕左上方布局组件。
表格布局(TableLayout):按照行列方式布局组件。
相对布局(RelativeLayout):相对其它组件的布局方式。
绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
布局的一些基本属性:
一:线性布局LinearLayout
线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。
常用的属性:
android:orientation:设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:权重(控制各个组件在布局中的相对大小比例)
简单线性布局中布局嵌套的应用:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.jc.myapp.MainActivity" android:weightSum="1"> <!--match_parent:跟随屏幕大小--> <!--wrap_content:跟随内容大小--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.3" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左上按钮" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右上按钮" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中间按钮" android:layout_gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.3"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左下按钮" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右下按钮" /> </LinearLayout> </LinearLayout></LinearLayout>
效果图
二:表格布局TableLayout
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
简单表格布局应用:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第一个lay的第一行" android:background="#ff0000" android:textColor="#aba6a6" android:layout_weight="1" android:layout_span="2" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2行1列" android:background="#ff0000" android:textColor="#aba6a6" android:layout_weight="1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2行2列" android:background="#ffffff" android:textColor="#aba6a6" android:layout_weight="2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2行3列" android:background="#ff00fb" android:textColor="#aba6a6" android:layout_weight="3" /> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="1" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个layout第1行第1列" android:background="#00ff15" android:textColor="#aba6a6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="c" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个layout第1行第3列" android:background="#dd00ff" android:textColor="#aba6a6" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2行1列" android:background="#00ff15" android:textColor="#aba6a6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="c" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2行3列" android:background="#dd00ff" android:textColor="#aba6a6" /> </TableRow> </TableLayout></LinearLayout>
效果图:
三:网格布局GridLayout
作为android 4.0 后新增的一个布局,与前面介绍过的TableLayout(表格布局)其实有点大同小异;
不过新增了一些东西
①跟LinearLayout(线性布局)一样,他可以设置容器中组件的对齐方式
②容器中的组件可以跨多行也可以跨多列(相比TableLayout直接放组件,占一行相比较)
因为是android 4.0新增的,API Level 14,在这个版本以前的sdk都需要导入项目
常用属性:
android:rowCount=”5” 设置网格布局中几行
android:columnCount=”4” 设置网格布局中几列
android:layout_columnSpan=”2” 跨列
android:layout_gravity=”fill_horizontal” 垂直填充
android:layout_columnSpan=”3” 跨行
android:layout_gravity=”fill_horizontal” 水平填充
scaleType=”fitXY” 设置在x轴和y轴填满
网格布局简单应用:
<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="5" android:columnCount="4" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="x" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:layout_columnSpan="3" android:layout_gravity="fill_horizontal" /> <Space /></GridLayout>
效果图:
四:帧布局(框架布局)FrameLayout
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
帧布局简单应用:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/a3" android:scaleType="fitXY" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/a2" android:layout_gravity="center" /></FrameLayout>
效果图:
总结:
本文对Android五大布局介绍&属性设置进行了基础简单的介绍。接下来我会介绍继续介绍Android开发中的相关知识。
- Android布局学习笔记
- Android 布局学习笔记
- Android学习笔记--布局
- android学习笔记-布局
- Android 学习笔记-布局
- Android学习笔记之布局
- Android学习笔记 - 布局篇
- Android学习笔记 - 布局篇
- Android学习笔记TableLayout布局
- android学习笔记1-布局
- Android UI学习笔记------布局
- android学习笔记(三)--布局
- Android学习笔记之布局
- Android的布局学习笔记
- android学习笔记---UI布局
- Android XML布局学习笔记
- [学习笔记]Android基本布局
- Android 学习笔记-布局2
- 学习Linux命令(18)
- 电商 购物车-订单-支付流程
- 【Python】7“函数定义和使用“
- var_dump()返回和mysqli_result 类中成员方法
- [LeetCode] Maximum Product Subarray 求连续子数组的最大乘积
- Android 学习笔记-布局
- PHP获得一些随机上传名字防止重命名
- Vjudge攻略——ACdream-1099
- Python学习笔记(9) -- string.letters(关键词:a-zA-Z的集合)
- myql 统计优化
- MySQL 学习<十二> MySQL权限表
- 【读书笔记】机器学习实战 第7章 基于单层决策树的adaboost
- 将字符串转换成整数[Java]
- JavaWeb--使用Websocket实现在线聊天功能