Android 之布局(一)
Android的布局类型:
主要有:LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、AbsoluteLayout(绝对布局)、FrameLayout(单帧布局)。
1、LinearLayout(线性布局):是5种布局最常用的一种,这种布局在显示组件的时候会默认保持组件之间的间隔以及组件之间的互相对齐。
(1)显示组件两种方式:垂直(vertical)和水平(horizontal),是通过orientation来设定的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" //垂直排列(水平为horizontal)
> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button3"
/> </LinearLayout>
(2)可嵌套使用线性布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button3"
/> </LinearLayout>
</LinearLayout>
总结:线性布局里头一些常用的属性如下
1)android:id - 为控件指定相应的ID(例如:android:id="@+id/txt" )
2)android:text - 指定控件当中显示的文字,注意,尽量使用string.xml(例如android:text = "@string/hello_world")
3)android:gravity - 指定控件的基本位置,比如说居中,居右等位置 (例如android:gravity="center_vertical" )
android:layout_gravity-是相对与它的父元素说的,说明元素显示在父元素的什么位置。
每个组件默认其值为左上角对齐,其属性可以调整组件对齐方式比如向左、向右或者居中对齐等。
可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且这些属性是可以多选的,用“|”分开。默认这个的值是:Gravity.LEFT。
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。
4)android:textSize - 指定控件当中字体的大小 (例如android:textSize="15pt" )
5)android:background - 指定控件所用的背景色,RGB命名法(android:background="#aa0000" )
6)android:layout_width - 指定控件的宽度(例如android:layout_width="fill_parent" )
一般是"fill_parent","wrap_content","match_parent",也可以设置数值。
7) android:layout_height - 指定控件的高度(例如android:layout_height="wrap_content" )
一般是"fill_parent","wrap_content","match_parent",也可以设置数值。
8)android:layout_weight - 指定控件的占用比例(例如android:layout_weight="2" )
默认为零,其属性表示当前还有多大视图就占据多大视图;如果其值高于零,则表示将父视图中的可用的空间进行分割,分割的大小视当前屏幕整体布局的Layout_weight值与每个组件Layout_weight值占用比例来定。
9)android:padding - 指定控件的内边距,也就是说控件当中的内容 (例如:android:paddingLeft="10dip" )
共有paddingTop、paddingBottom、paddingLeft、paddingRight、padding五种。
10)android:sigleLine - 如果设置为真的话,则将控件的内容显示在一行当中(例如android:singleLine="true" )
11)android:layout_margin,外边距,其上下左右属性为:layout_marginTop,layout_marginBottom,layout_marginLeft,layout_marginRight
注意:线性布局中
[1]带"layout"的属性是指整个控件与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;
[2]不带"layout" 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性.
2、RelativeLayout(相对布局)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/btn1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/btn2"
android:layout_below="@id/btn1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:id="@+id/btn3"
android:layout_below="@id/btn2"
android:layout_alignRight="@id/btn2"
/>
</RelativeLayout>
总结:相对布局常用属性
(1)组件之间的位置关系:
[1]android:layout_above :将该控件的底部至于给定ID的控件之上;
[2]android:layout_below :将该控件的顶部至于给定ID的控件之下;
[3]android:layout_toLeftOf :将该控件的右边缘和给定ID的控件的左边缘对齐;
[4]android:layout_toRightOf :将该控件的左边缘和给定ID的控件的右边缘对齐;
(2)组件对齐方式:
[1]android:layout_alignBaseline:该控件的baseline和给定ID的控件的baseline对齐;
[2]android:layout_alignBottom:将该控件的底部边缘与给定ID控件的底部边缘对齐;
[3]android:layout_alignLeft:将该控件的左边缘与给定ID控件的左边缘对齐;
[4]android:layout_alignRight:将该控件的右边缘与给定ID控件的右边缘对齐;
[5]android:layout_alignTop:将给定控件的顶部边缘与给定ID控件的顶部对齐;
(3)当前组件与父组件的对齐方式:
[1]android:alignParentBottom:true,则将该控件的底部和父控件的底部对齐;
[2]android:layout_alignParentLeft:true,则将该控件的左边与父控件的左边对齐;
[3]android:layout_alignParentRight:true,则将该控件的右边与父控件的右边对齐;
[4]android:layout_alignParentTop:true,则将空间的顶部与父控件的顶部对齐;
(4)组件放置的位置:
[1]android:layout_centerHorizontal:true,该控件将被至于水平方向的中央;
[2]android:layout_centerInParent:true,该控件将被至于父控件水平方向和垂直方向的中央;
[3]android:layout_centerVertical:true,该控件将被至于垂直方向的中央;
Android 之布局(一)的更多相关文章
- 【腾讯Bugly干货分享】Android动态布局入门及NinePatchChunk解密
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57c7ff5d53bbcffd68c64411 作者:黄进——QQ音乐团队 摆脱 ...
- Xamarin.Android之布局文件智能提示问题
一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...
- android—-线性布局
android五大布局之线性布局. 1.线性布局的特点:各个子元素彼此连接,中间不留空白 而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用 用的比较多的就是L ...
- Android基本布局
android基本布局有三种:LinearLayout,RelativeLayout,FrameLayout. 一.LinearLayout 1,这是一种垂直布局(或者水平布局),可以通过下面这一句来 ...
- android layout布局属性
参考:http://blog.csdn.net/msmile_my/article/details/9018775 第一类:属性值 true或者 false android:lay ...
- Android 学习第10课,Android的布局
Android的布局 线性布局
- Android 优化布局层次结构
前面介绍过使用HierarchyViewer和Android lint来优化我们的程序,这一篇算是总结性的,借助一个小例子来说用怎么优化应用布局.这个例子是android官网给出的,作者也当一把翻译. ...
- Android 五大布局
Android 五大布局: FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),Table ...
- Android界面布局基本知识简述
Android手机操作系统在模拟器中进行相关的编写,可以帮助我们实现各种功能需求.尤其是在界面的操作方面显得更为突出.在这里我们就可以对Android界面布局的相关操作来对这方面的知识进行一个深入的了 ...
- android的布局管理器
理论上通过setContentView(view)能够把一个view设置到activity中,但当你有很多个view控件的时候,就需要用android的布局管理器来管理view控件了. android ...
随机推荐
- 【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解
A. Gudako and Ritsuka 链接 by Yuki & Asm.Def 期望难度:Hard- 考虑从后往前进行博弈动态规划,在这一过程中维护所有的先手必胜区间.区间不妨采用左开右 ...
- python开发_webbrowser_浏览器控制模块
''' python的webbrowser模块支持对浏览器进行一些操作 主要有以下三个方法: webbrowser.open(url, new=0, autoraise=True) webbrowse ...
- UVA 11945 Financial Management 水题
Financial Management Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 acm.hust.edu.cn/vjudge/problem/vis ...
- 双频无线网安装设置(5g ) for linux
为了在局域网实现远程wifi调试,例如调试需要图像数据传输,则需要搭建局域网5g无线网络. 1.硬件要求 a. TP-Link(型号:TL-WDR6500,AC1300双频无线路由器,支持5g,2.4 ...
- .NET快速查找某个类所在的命名空间
有时候我们从网上copy别人的代码下来,对于某些不熟悉的类,需要添加对某个类的引用时,如何快速找出某个类所在的命名空间呢 例如有如下的一段代码: 现在要添加ConfigurationElement类的 ...
- php Function split() is deprecated 的解决办法
原文地址: http://www.cnblogs.com/mfryf/archive/2012/05/31/2527307.html php升级为5.3后,程序会报 Function split() ...
- DevExpress 利用DateEdit 仅显示和选择年份 z
DevExpress只提供了选择月份的控件MonthEdit,并没提供选择选择年份的控件,目测是官方偷懒不想弄,因为要实现的方法也很简单,利用ComboBoxEdit添加年份数据即可,直接封装一个控件 ...
- 精选 5 个漂亮的 CSS3 图片滑过特效
这篇文章将为大家分享5款漂亮的CSS3图片滑过特效,比如滑过后显示图片的详细文字介绍,又比如滑过后对图片进行淡入淡出的效果等等.让我们一起来看看,喜欢的朋友赶紧收藏. 1.非常酷的CSS3图片说明效果 ...
- coursera课程Text Retrieval and Search Engines之Week 3 Overview
Week 3 OverviewHelp Center Week 3 On this page: Instructional Activities Time Goals and Objectives K ...
- Windows + IIS 环境部署Asp.Net Core App
环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运 ...