android常用讲义布局介绍
- 格式:ppt
- 大小:752.00 KB
- 文档页数:20
Android系统五大布局Android系统中为我们提供了五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。
其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。
这些布局都可以嵌套使用。
(一)RelativeLayout相对布局相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。
它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。
下面是常用的一些属性RelativeLayout用到的一些重要的属性:第一类:属性值为true或falseandroid:layout_centerHrizontal水平居中android:layout_centerVertical垂直居中android:layout_centerInparent相对于父元素完全居中android:layout_alignParentBottom贴紧父元素的下边缘android:layout_alignParentLeft贴紧父元素的左边缘android:layout_alignParentRight贴紧父元素的右边缘android:layout_alignParentTop贴紧父元素的上边缘android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物第二类:属性值必须为id的引用名“@id/id-name”android:layout_below在某元素的下方android:layout_above在某元素的的上方android:layout_toLeftOf在某元素的左边android:layout_toRightOf在某元素的右边android:layout_alignTop本元素的上边缘和某元素的的上边缘对齐android:layout_alignLeft本元素的左边缘和某元素的的左边缘对齐android:layout_alignBottom本元素的下边缘和某元素的的下边缘对齐android:layout_alignRight本元素的右边缘和某元素的的右边缘对齐第三类:属性值为具体的像素值,如30dip,40pxandroid:layout_marginBottom离某元素底边缘的距离android:layout_marginLeft离某元素左边缘的距离android:layout_marginRight离某元素右边缘的距离android:layout_marginTop离某元素上边缘的距离EditText的android:hint设置EditText为空时输入框内的提示信息。
Android开发的六种常⽤布局本⼈是Android⼩⽩,刚刚学习Android开发,通过观看教学视屏和书籍学习,总结下Android开发的⼩知识,⼤神勿喷!第⼀种:LinearLayout:线性布局 线性布局是按照处置或者⽔平进⾏排布的,默认是⽔平 属性:orientation:⽤来指定当前的线性布局的排布⽅向。
wrap_content:包裹内容 match_parent:匹配⽗类 margin:外边距 padding:内边距 gravity:便是控件的内部内容针对控件本⾝的对其⽅式 layout_gravity:表⽰该控件⾃⾝在⽗类布局中的对齐⽅式 注:如果线性布局排布⽅式为⽔平,那么layout_gravity在⽔平的⽅向上就不起作⽤,只能在垂直的⽅向上起作⽤ layout_weight:表⽰权重的概念,即该百分⽐的形式进⾏对齐⽅式 如果控件划分的区域为wrap_content,那么权重值越⼤,所占⽐例就会越⼤ 如果控件划分的区域为match_parent,那么权重值越⼤,所占⽐例就会越⼩思考:在⼀个页⾯中,有⼀个权重的控件和⼀个⽆权重的控件,系统会先给谁分配空间尼? 答:系统肯定先给⽆权重的控件分配空间,才会得到需要百分⽐的空间⼤来进⾏百分⽐的划分。
1<?xml version="1.0" encoding="utf-8"?>2<LinearLayout xmlns:android="/apk/res/android"3 xmlns:tools="/tools"4 android:layout_width="match_parent"5 android:layout_height="match_parent"6 android:orientation="vertical"7>89<Button10android:layout_width="match_parent"11 android:layout_height="wrap_content"12 android:paddingTop="20dp"13 android:paddingBottom="20dp"14 android:layout_marginTop="20dp"15 android:layout_marginBottom="20dp"16 android:text="Button1"/>1718<Button19android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:paddingTop="20dp"22 android:paddingBottom="20dp"23 android:paddingLeft="30dp"24 android:paddingRight="30dp"25 android:layout_gravity="center"26 android:text="Button2"/>2728</LinearLayout>第⼆种:RelativeLayout:绝对布局按照空间之间的相对位置进⾏排布,存在⼀个参照物的概念。
Android系统五大布局详解我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的。
组件就是我们常见的Button、TextEdit等等。
那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。
在分析布局之前,我们首先看看控件:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。
第二种是我们在代码中直接使用相应的类来创建视图。
如何使用XML文件定义视图:每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。
布局文件一般以对应activity的名字命名,以 .xml 为后缀。
在xml中为创建组件时,需要为组件指定id,如:android:id="@+id/名字"系统会自动在gen目录下创建相应的R资源类变量。
如何在代码中使用视图:在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。
如:private Button btnSndMag;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main); // 加载main.xml布局文件btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件}下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。
Android培训之五大布局讲解Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。
组件按照布局的要求依次排列,就组成了用户所看见的界面。
Android的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。
LinearLayout:LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。
如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。
如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。
LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。
加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。
如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。
FrameLayout:FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。
显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。
Android基础知识及线性布局介绍⽬录1.常见控件的基本属性1.1控件的可见性1.2控件的外边距1.3控件的内边距2.线性布局(Linear Layout)2.1⽰例:2.2微信界⾯实战3.总结1.常见控件的基本属性android:id="@+id/button1":【设置控件id】android:layout_width【设置控件宽度】/android:layout_height【设置控件⾼度】wrap_content【控件的⼤⼩由内部决定】match_parent【控件的⼤⼩与⽗控件保持⼀致】android:text=" ":【设置组件⽂本】android:textColor=" ":【设置字体颜⾊】android:layout_marginLeft:【当前布局与⽗布局左边缘的距离】android:layout_marginRight:【当前布局与⽗布局右边缘的距离】android:layout_marginTop:【当前布局与⽗布局顶部边缘的距离】android:layout_marginBottom:【当前布局与⽗布局底部边缘的距离】android:gravity :【view⾥⾯的内容在这个view中的位置】android:layout_gravity :【这个view相对于它⽗view的位置】1、gravity在线性布局中不起任何作⽤,layout_gravity在线性布局中起作⽤;2、当我们使⽤ android:orientation=“vertical” 时, android:layout_gravity只有⽔平⽅向的设置才起作⽤,垂直⽅向的设置不起作⽤。
即:left,right,center_horizontal 是⽣效的;3、当我们使⽤android:orientation=“horizontal” 时, android:layout_gravity只有垂直⽅向的设置才起作⽤,⽔平⽅向的设置不起作⽤。
第2章 Android常见界面布局《Android移动开发》学习目标/Target了解View与ViewGroup的简介,能够说出View与ViewGroup的作用和关联掌握界面布局在XML文件中与Java代码中的编写方式,能够独立编写界面布局掌握编写简单Android程序的步骤,能够编写一个HelloWorld程序掌握常见界面布局的特点及使用,能够搭建简单的界面布局章节概述/ Summary在Android应用中,界面由布局和控件组成。
布局好比是建筑里的框架,控件相当于建筑里的砖瓦。
针对界面中控件不同的排列位置,Android定义了相应的布局进行管理。
本章将针对Android界面中常见的布局进行详细地讲解。
01 02 03 04View视图界面布局编写方式界面布局的通用属性线性布局05 06相对布局表格布局07帧布局2.1View视图先定一个小目标!了解View与ViewGroup的简介,能够说出View与ViewGroup的作用和关联所有的UI元素都是通过View与ViewGroup构建的,对于一个Android应用的用户界面来说,ViewGroup作为容器盛装界面中的控件,它可以包含普通的View控件,也可以包含ViewGroup。
2.2界面布局编写方式2.2 界面布局编写方式先定一个小目标!掌握在XML文件中编写布局,能够搭建简单的布局界面掌握在Java代码中编写布局,能够搭建简单的布局界面2.2 界面布局编写方式在实现Android界面效果之前,我们首先需要编写界面布局,界面布局的编写方式有2种,第1种是在XML文件中编写布局,第2种是在Java代码中编写布局。
Ø在XML文件中编写布局:推荐此种方式编写布局u有效的将界面中的布局代码与Java代码隔离,使程序的结构更加清晰。
Ø在Java代码中编写布局u在Android中所有布局和控件的对象都可以通过new关键字创建出来,将创建的View控件添加到ViewGroup布局中,从而实现View控件在布局界面中显示。
Android开发之基本控件和详解四种布局⽅式Android中的控件的使⽤⽅式和iOS中控件的使⽤⽅式基本相同,都是事件驱动。
给控件添加事件也有接⼝回调和委托代理的⽅式。
今天这篇博客就总结⼀下Android中常⽤的基本控件以及布局⽅式。
说到布局⽅式Android和iOS还是区别挺⼤的,在iOS中有Frame绝对布局和AutoLayout相对布局。
⽽在Android中的布局⽅式就⽐较丰富了,今天博客中会介绍四种常⽤的布局⽅式。
先总结⼀下控件,然后再搞⼀搞基本⽅式,开发环境还是⽤的Mac下的Android Studio。
开始今天的正题, 虽然Android的控件和布局⽅式都可以拖拽实现,今天为了更详细的了解控件和布局,我们就⽤纯代码的形式来进⾏实现和介绍。
⼀、常⽤基本控件1.TextView看到Android中的TextView, 我不禁的想到了iOS开发中的UILabel。
从字⾯意思上看,TextView就是⽂本视图,只是⽤来显⽰⽂字的。
在iOS 中就叫做标签,即为UILabel。
要想在Activity中显⽰TextView, 我们需要在相应的布局⽂件,也就是Activity对应的layout.xml⽂件去添加相应的控件标签。
这些xml标签可以确定控件的位置,⼤⼩,颜⾊等属性。
下⽅是在Activity中显⽰⼀个TextView。
布局代码如下:1<TextView2android:id="@+id/name_text_view"3 android:layout_width="match_parent"4 android:layout_height="wrap_content"5 android:gravity="center"6 android:textSize="30sp"7 android:textColor="#be0e0a"8 android:text="My name is ZeluLi"/>标签<TextView/>代表着我们要在Activity中添加⼀个个TextView, 标签中可以设置⼀些属性。
安卓的主要几大布局今天我们的主要内容就是安卓的主要几个基础的布局方式。
(主要布局如下:)1.线性布局(LinerLayout)2.相对布局(RelativeLayout)3.表格布局(TableLayout)4.网格布局(GridLayout)5.绝对布局(AbsoluteLayout)6.帧布局(FrameLayout)一:线性布局(LinerLayout)。
1.xml文件配置:<?xml version="1.0" encoding="utf-8"?><!-- 线性布局的页面 --><LinearLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#0000FF"android:gravity="center_horizontal|center_vertical"android:text="线性布局"android:textSize="20sp"/><LinearLayoutandroid:layout_width="200dp"android:layout_height="150dp"android:layout_gravity="center_horizontal|center_vertical"android:background="#0000FF"android:orientation="horizontal" ><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:background="#00FF00"android:text="内容一" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="#F7F709"android:text="内容二" /></LinearLayout><LinearLayoutandroid:layout_width="200dp"android:layout_height="150dp"android:layout_gravity="right"android:background="#0000FF"android:orientation="horizontal" ><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="1"android:background="#00FF00"android:text="内容一" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="2"android:background="#F7F709"android:text="内容二" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="1"android:text="按一"android:onClick="click"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="1"android:text="按二"android:onClick="click"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="1"android:text="按三"android:onClick="click"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="1"android:text="按四"android:onClick="click"/></LinearLayout></LinearLayout>展示:2.xml文件配置:<?xml version="1.0" encoding="utf-8"?><!-- 线性布局页面 --><LinearLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center_horizontal|center_vertical"android:text="线性布局登录页面"android:textSize="20sp" /><LinearLayoutandroid:layout_width="200dp"android:layout_height="40dp"android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="20dp"android:orientation="horizontal" ><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="账号:"android:textSize="15sp" /><EditTextandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="4" /></LinearLayout><LinearLayoutandroid:layout_width="200dp"android:layout_height="40dp"android:layout_gravity="center_horizontal|center_vertical" android:orientation="horizontal" ><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="密码:"android:textSize="15sp" /><EditTextandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="4" /></LinearLayout><LinearLayoutandroid:layout_width="200dp"android:layout_height="30dp"android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="15dp"android:orientation="horizontal" ><CheckBoxandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="记住账号" /><CheckBoxandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="记住密码" /></LinearLayout><LinearLayoutandroid:layout_width="200dp"android:layout_height="30dp"android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="15dp"android:orientation="horizontal" ><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="click"android:text="登录"android:textSize="10sp" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textSize="15sp" /><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="click"android:text="注册"android:textSize="10sp" /></LinearLayout></LinearLayout>展示:二:相对布局(RelativeLayout)1.xml文件布局如下:<?xml version="1.0" encoding="utf-8"?><!-- 相对布局页面 --><RelativeLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center"android:text="相对布局登录页面"android:textSize="20sp" /><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/textView"android:layout_marginLeft="80px"android:layout_marginTop="30dp"android:text="账号:"android:textSize="15sp" /><EditTextandroid:id="@+id/firstEditText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignBottom="@id/textView1"android:layout_marginLeft="130px"android:layout_marginRight="50px" /><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstEditText" android:layout_marginLeft="80px"android:layout_marginTop="20dp"android:text="密码:"android:textSize="15sp" /><EditTextandroid:id="@+id/firstEditText2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignBottom="@id/textView2" android:layout_marginLeft="130px"android:layout_marginRight="50px" /><CheckBoxandroid:id="@+id/checkbox1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/firstEditText2" android:layout_marginLeft="100px"android:layout_marginTop="20dp"android:text="记住账号" /><CheckBoxandroid:id="@+id/checkbox2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignBottom="@id/checkbox1" android:layout_marginLeft="280px"android:layout_marginRight="50px"android:text="记住密码" /><Buttonandroid:id="@+id/cancelButton1"android:layout_width="match_parent"android:layout_height="40dp"android:layout_below="@id/checkbox2"android:layout_marginLeft="100px"android:layout_marginRight="290px"android:layout_marginTop="25dp"android:text="登录"android:textSize="14sp" /><Buttonandroid:id="@+id/confremButton2"android:layout_width="match_parent"android:layout_height="40dp"android:layout_alignBottom="@id/cancelButton1" android:layout_marginLeft="290px"android:layout_marginRight="100px"android:text="注册"android:textSize="14sp" /></RelativeLayout>展示:三:表格布局(TableLayout)xml文件如下:<?xml version="1.0" encoding="utf-8"?><!-- 表格布局页面 --><TableLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"android:stretchColumns="2" ><TableRow><TextViewandroid:layout_width="50dp"android:layout_height="wrap_content"android:gravity="center"android:text="序号"android:textSize="20sp" /><TextViewandroid:layout_width="60dp"android:layout_height="wrap_content"android:gravity="center"android:text="书名"android:textSize="20sp" /><TextViewandroid:gravity="center"android:text="内容"android:textSize="20sp" /><TextViewandroid:layout_width="60dp"android:layout_height="wrap_content"android:gravity="center"android:text="作者"android:textSize="20sp" /></TableRow><Viewandroid:layout_height="1.5dp"android:background="#00FF00" /><TableRow><TextViewandroid:gravity="center"android:text="1" /><TextViewandroid:gravity="center"android:text="西游记" /><TextViewandroid:gravity="center"android:text="取经" /><TextViewandroid:gravity="center"android:text="郑晨" /></TableRow><Viewandroid:layout_height="1.5dp"android:background="#00FF00" /><TableRow><TextViewandroid:gravity="center"android:text="2" /><TextViewandroid:layout_column="2"android:gravity="center"android:text="孙悟空大闹天空" /> </TableRow><Viewandroid:layout_height="1.5dp" android:background="#00FF00" /><TableRow><TextViewandroid:gravity="center"android:text="3" /><TextViewandroid:layout_column="1" android:gravity="center"android:text="盘龙" /><TextViewandroid:layout_column="3" android:gravity="center"android:text="郑晨" /></TableRow><Viewandroid:layout_height="1.5dp" android:background="#00FF00" /></TableLayout>展示:四:网格布局(GridLayout)1.xml文件如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp" ><!-- 网格布局 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:gravity="center"android:text="计算机"android:textSize="30sp" /><GridLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="20dp"android:columnCount="4"android:rowCount="6" ><EditTextandroid:id="@+id/textView6"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_columnSpan="4"android:layout_gravity="fill_horizontal" android:editable="false"android:focusable="false"android:gravity="right"android:text="0"android:textSize="20sp" /><Button android:text="AC"android:onClick="acClick"/><Button android:text="+/-" /><Button android:text="%" /><Button android:text="+" /><Button android:text="7" /><Button android:text="8" /><Button android:text="9" /><Button android:text="x"android:onClick="anClick" /><Button android:text="4" /><Button android:text="5" /><Button android:text="6"android:onClick="bnClick" /><Button android:text="-" /><Button android:text="1"android:onClick="onClick" /><Button android:text="2" /><Button android:text="3" /><Button android:text="+" /><Buttonandroid:layout_columnSpan="2"android:layout_gravity="fill_horizontal"android:text="0" /><Button android:text="." /><Button android:text="=" /></GridLayout></LinearLayout>2.Activity如下(这是我只写了几个按键可以按,作为例子)://网格布局public class GridLayout extends Activity {private EditText editText;private boolean lastClickIsNumber = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.grid_layout);editText = (EditText) findViewById(R.id.textView6); }public void acClick(View v) {}public void onClick(View v) {if (!lastClickIsNumber) {editText.setText("1");} else {String oldContent = editText.getText().toString().trim(); oldContent += "1";editText.setText(oldContent);}lastClickIsNumber = true;}public void anClick(View v) {if (!lastClickIsNumber) {editText.setText("x");} else {String oldContent = editText.getText().toString().trim(); oldContent += "x";editText.setText(oldContent);}lastClickIsNumber = true;}public void bnClick(View v) {if (!lastClickIsNumber) {editText.setText("6");} else {String oldContent = editText.getText().toString().trim(); oldContent += "6";editText.setText(oldContent);}lastClickIsNumber = true;}}展示:五.绝对布局(AbsoluteLayout)xml文件如下:<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:padding="5dp"><TextViewandroid:layout_width="100dp"android:layout_height="100dp"android:layout_x="100dp"android:layout_y="50dp"android:text="你好吗?"/><TextViewandroid:layout_width="100dp"android:layout_height="100dp" android:layout_x="50dp"android:layout_y="100dp"android:text="我很好!"/><TextViewandroid:layout_width="100dp" android:layout_height="100dp" android:layout_x="200dp"android:layout_y="100dp"android:text="真的吗?"/><TextViewandroid:layout_width="100dp" android:layout_height="100dp" android:layout_x="100dp"android:layout_y="200dp"android:text="真的!"/></AbsoluteLayout>展示:。
#000000 黑色#FFFFFFFF 完全不透明#00FFFFFF 完全透明#88FFFFFF 半透明6.android:layout_weight 设置控件的权重,即各控件在水平或者垂直方向上平均分配。
设置格式:.android:layout_weight="1"备注:如果是水平方向设置权重,要将android:layout_width设置为0dp,如果是垂直方向上使用权重,要将android:layout_height设置为0dp(否则权重容易受到高度或宽度的干扰而出现偏差)7.android:gravity 该属性用来控制该View的内容物的位置。
可选项:top、bottom、left、right、center_vertical、fill_vertical 、 center、fill等等备注:本属性与android:layout_gravity不同。
如果该属性是定义在布局节点中,则该布局中所有控件的位置都受到这个属性的控制。
如果该属性出现在Button、TextView、EditText等控件中,则用来控制这些控件上的文字的位置。
8.android:layout_gravity 该属性用于设置控件相对于容器的对齐方式。
可选项有:top、bottom、left、right、center_vertical、center_horizontal 、fill_vertical 、 center、fill等等。
备注:本属性与android:gravity不同。
这些可选项中不是适用于每一种布局。
(在垂直线性布局中,android:gravity为bottom不起作用;而水平线性布局中,android:gravity为right不起作用)四、RelativeLayout:【掌握】(一)、概念:指按着控件之间的相对位置来进行布局。
(二)、RelativeLayout特有属性:1、android:gravity设置布局容器内子控件的对齐方式2、android:ignoreGravity设置布局管理器内哪个控件不受gravity属性的影响(三)、RelativeLayout子元素的特有属性:LayoutParamsA、第一组:指控件与父布局之间的对齐关系。
Android系统常用布局介绍Android中的布局包括线性布局,表格布局,相对布局,帧布局,绝对布局。
下面分别对每个布局进行一个介绍。
在介绍android的布局管理器之前,有必要了解android平台下的控件类。
首先要了解的是View类,该类为所有可视化控件的基类,主要提供了控件绘制和事件处理的方法。
创建用户界面所使用的控件都继承自View,如TextView,Button,CheckBox等。
补充:另外一个需要了解的是ViewGroup类,它也是View类的子类,但是可以充当其他控件的容器。
ViewGroup的子控件既可以是普通的View,也可以是ViewGroup。
Android中的一些高级控件如Gally,GirdView等都继承自ViewGroup。
一:线性布局:LinearLayout类简介线性布局是最简单的布局之一,它提供了控件水平或者垂直排列的模型。
同时,使用此布局时可以通过设置控件的weight参数控制各个控件在容器中的相对大小。
LinearLayout布局的属性既可以在布局文件(XML)中设置,也可以通过成员方法进行设置。
案例:1.在布局管理器中实现布局:在eclipse中新建一个项目。
首先打开项目文件夹下res/values目录下的string.xml,写入一下内容:<?xml version=”1.0”encoding=”utf-8”?><resources><string name=”app_name”>LinearExample</string><string name=”button”>按钮</string><string name=”add”>添加</string></resources>说明:在string.xml中主要声明了程序中要用到的字符串资源,这样将所有字符串资源统一管理有助于提高程序的可读性及可维护性。
中博软件学院——基础五1:请介绍下Android中常用的五种布局答案:Android中常用的五种布局分别是:FrameLayout(框架布局)、LinearLayout(线性布局)、AbsoluteLayout(绝对布局)、RelativeLayout(相对布局)、TableLayout(表格布局)一、FrameLayout:所有东西依次都放在左上角,会重叠,这个布局比较简单,也只能放一点比较简单的东西。
二、LinearLayout:线性布局,每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。
当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。
三、AbsoluteLayout:绝对布局用X,Y坐标来指定元素的位置,这种布局方式也比较简单,但是在屏幕旋转时,往往会出问题,而且多个元素的时候,计算比较麻烦。
四、RelativeLayout:相对布局可以理解为某一个元素为参照物,来定位的布局方式。
主要属性有:相对于某一个元素android:layout_below、android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh。
五、TableLayout:表格布局,每一个TableLayout里面有表格行T ableRow,TableRow 里面可以具体定义每一个元素。
2:请描述下Activity的生命周期答案:Acitvity的生命周期与activity的活动状态密切相关,也和activity的周期函数密切相关,共有七个周期函数:onCreate()、onStart()、onRestart()、onResume()、onPause()、onStop()、onDestroy()完整生命周期:即一个activity从出现到消失,对应的周期方法是从onCreate()到onDestroy();onCreate()方法在创建activity时调用。
Android 之常见布局二在Android开发中还有其它的布局方式,如表格布局(TableLayout),绝对布局(Absolutelayout)以及帧布局(FrameLayout),下面我们来一一介绍。
1.表格布局把子控件放到行与列中,不显示行和列以及单元格的边界线,而且单元格之间不能跨越,如html中的表格一样。
每一行都是一个TableRow对象。
如图1-1是一个表格布局的效果图:图1-1所示图1-1可以看作该表格布局中有三行,且每一行的格式都是一样的,图1-2是其中一行<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent" ><TableRowandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:" /><EditTextandroid:layout_width="200dip"第一行第二行第三行整个表格布局定义表格布局的根节点定义表格布局中的某一行android:layout_height="wrap_content" /></TableRow>图1-2所示2.绝对布局绝对布局的子控件需要指定相对于此坐标布局的横纵坐标值,否则在页面添加的布局都会叠加在同一个位置。