android布局及其常用属性
Android布局:
Android的界面是由布局和组件协同完成的,布局好比造房子的框架,而组件就是造房子用到的砖瓦。
所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类:线、相、绝、表、帧。
1、线性布局(LinearLayout)
①简介:按照垂直或者水平方向布局的组件。
②常用的属性:
android:orientation: (方向)可以设置布局的方向
android:gravity: (重心)用来控制组件的对齐方式
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:orientation="vertical"><!--先是一个线性布局的输入框--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><!-- 然后是线性布局的按钮,主要介绍gravity属性--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="right"android:orientation="horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Yes" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="No" /></LinearLayout><!--最后是线性布局的文本框,主要介绍layout_weight属性--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#0000aa"android:text="blue" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="#aa0000"android:text="red" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="3"android:background="#00aa00"android:text="green" /></LinearLayout></LinearLayout>
2、相对布局(RelativeLayout)
①简介:相对其他组件的布局方式。
②常用属性:
可参见:http://blog.csdn.net/fifteen718/article/details/49025185
③实例效果图:
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><!--还是和上个界面一样,这次用相对布局来实现--><EditTextandroid:id="@+id/tx1"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_below="@id/tx1"android:text="No" /><Buttonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/tx1"android:layout_toLeftOf="@id/btn1"android:text="Yes" />
</RelativeLayout>
3、绝对(坐标)布局(AbsoluteLayout)
①简介:按照绝对坐标来布局组件。
②绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。
4、表格布局(TableLayout)
①简介:按照行列方式布局的组件。
②常用属性:
android:collapseColumns: 隐藏指定的列
android:shrinkColumns: 收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns: 尽量把指定的列填充空白部分
android:layout_column: 控件放在指定的列
android:layout_span: 该控件所跨越的列数
③实例效果图:
代码:
<?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><Button android:text="111" /><Button android:text="222" /><Button android:text="333" /><Button android:text="444" /></TableRow><TableRow><Button android:text="555" /><!--用layout_span来设置跨2列--><Buttonandroid:layout_span="2"android:text="666" /></TableRow></TableLayout>
5、帧布局(FrameLayout)
①简介:从屏幕左上方布局的组件。
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
②常用属性:
android:foreground: 设置该帧布局容器的前景图像
android:foregroundGravity: 设置前景图像显示的位置
③实例效果图:
代码:
<FrameLayout 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:foreground="@drawable/frame"android:foregroundGravity="center"tools:context=".MainActivity"><!--一开始设置了一张居中的前景图,注意前景图会遮住该布局中的所有控件--><!--设置一个蓝色背景的文本框--><TextViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="#0000aa" /><!--绿色--><TextViewandroid:layout_width="150dp"android:layout_height="match_parent"android:background="#00aa00" /><!--红色--><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:background="#aa0000" /></FrameLayout>
最后补充:
现在也有六大布局之说,那是因为Android4.0之后又新增了一位成员:GridLayout(网格布局)
简介:控件从左到右,从上到下排列。
由于用的较少,暂时不详细介绍了。
转载于:https://www.cnblogs.com/fifteen718/p/9533981.html
android布局及其常用属性相关推荐
- android linearlayout属性大全,Android中LinearLayout布局的常用属性总结读书笔记
8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 原CSDN博客已弃用,文章会逐渐迁移过来. 应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程 ...
- Android:相对布局RelativeLayout常用属性
RelativeLayout中子控件常用属性: 1.相对于父控件,例如:android:layout_alignParentTop="true" ...
- Mono for Android布局控件属性小结
1. layout_weight 用于给一个线性布局中的诸多视图的重要度赋值. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间.若赋一个高于 ...
- css--DIV+CSS布局、常用属性、盒模型、自定义字体、标签的分类及相互转换
目录 一.DIV+CSS布局 1.1 布局时常用的属性 1.2 布局 - 上中下结构 1.3 布局 - 上中下 - 左右 1.4 圣杯布局和双飞翼布局 二.字体属性 2.1 字体颜色 2.2 字体大 ...
- android中用代码设置edittext属性为密码,Android中EditText常用属性设置
EditText继承关系:View–>TextView–>EditText 常用属性如下:android:layout_gravity="center_vertical" ...
- android imageview 的常用属性,android imageview scaletype属性
android imageview scaletype属性并不是很常用,以至于到现在我才注意到,但是在某些应用中,这是一个很重要的属性,比如相册应用. ImageView的Scaletype决定了图片 ...
- Android布局之weight属性解析
转自:http://blog.csdn.net/kangyaping/article/details/7400281 在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情 ...
- CSS布局最常用属性float(浮动)和position(定位)
在进行CSS网页布局时,对元素与容器进行布置与规划,最常用的两个属性就是浮动float和定位position.这两个属性的理解对CSS网页布局的学习非常重要.我们52CSS.com就这两个属性的相关知 ...
- android中seekbar 常用属性列表,随笔–android中修改SeekBar控件默认显示-Fun言
布局文件中相关代码如下: android:id="@+id/seekbar" android:layout_width="match_parent" andro ...
- Android XML 不常用属性
android:imeOptions="actionDone" EditText 默认情况下软键盘右下角的按钮为"下一个",点击会到下一个输入框,保持软键盘 设 ...
最新文章
- 设计模式----单件模式(c++)
- pta 插入排序还是堆排序
- 将页脚保持在页面的底部——Javascript+Css实现
- python turtle库画图案-Python:turtle库的使用及图形绘制
- 锋利的JQuery —— DOM操作
- 如何理解signal函数声明
- [ARM] [linux master] 调试技术002 在linux下面使用mtrace来检查一般程序的内存溢出
- 科学计算机的用途,计算机最主要的用途之一就是科学计算,科学计算可分为两类...
- 河北省计算机2018单招试题答案,2018年河北省普通高职单招考试十类和高职单招对口电子电工类、计算机类联考命题、考试与评卷...
- 讲的是关于提高网站速度的内容|转自csdn
- 素数生成器的算法【PHP EDITION】
- qq表情包html插件,jQuery QQ表情插件jquery.qqFace.js
- 39个让你得到设计灵感的站点
- 毕设开题报告重要内容
- 13.1.X:ByteScout PDF Extractor SDK
- thermal系列(7)-Thermal配置和调试
- Win10以太网没有有效的IP配置解决方法
- Hibernate写代码流程小结
- 数模电路基础知识 —— 5. 常见电路符号说明(三极管)
- 人人都是极客半月刊:第 3 期
热门文章
- C# 找出实现某个接口的所有类
- JFFS2文件系统的移植
- mvc:annotation-driven/与mvc:default-servlet-handler/之间的一个问题(转)
- 怎样成为PHP 方向的一个合格的架构师(转)
- 基于visual Studio2013解决面试题之1404希尔排序
- 在Asp.net Mvc 3中使用swfupload
- [学习笔记]--ASP.Net MVC
- 使用Asp.net MVC源代码调试你的应用程序
- 《业务需求说明书》检查单 zz
- 户如何获得渗透服务---步骤与效果