- 浏览: 663742 次
- 性别:
- 来自: 安徽
文章分类
- 全部博客 (252)
- Html/Div+CSS (12)
- Js/Jquery (34)
- Flex (2)
- Ajax (3)
- Java (35)
- C# (15)
- Spring (16)
- Hibernate (13)
- Struts2 (12)
- Struts1 (7)
- DWR (1)
- iBatis/myBatis (9)
- Tag(JSTL、EL) (1)
- Android (44)
- SQL (7)
- SEO (7)
- Exception (3)
- Tool (10)
- Other (3)
- WebService (9)
- Apache (7)
- Ext (0)
- Utils (12)
- thinking in programme (2)
- Hadoop (0)
- ActiveMQ (0)
- HTML5/CSS3 (0)
- WPF (1)
- NodeJs (1)
- 设计模式 (0)
- 程序人生 (1)
- 随笔 (1)
- Linux (1)
- Load Balance (0)
最新评论
-
drinkjava2:
太复杂了而且不通用,利用ThreadLocal可完美解决这一问 ...
JDBC的多条件动态查询 -
u013107014:
multipartRequest.getFiles(" ...
多文件上传 by MultipartFile and Multiple -
liyys:
可惜没讲你mysql数据库的表的设计
iBatis入门 -
Mapple_leave:
效果还是挺不错的,谢谢了。
中文简体与繁体的转换 -
arcpad:
JS禁用浏览器退格键
为了更好的管理Android应用的用户界面里的个组件,Android提供了布局管理器,通过布局管理器,Android应用的图形用户界面具有良好的平台无关性。这里什么叫平台的无关性呢?就是说不同手机。我们知道不同手机它们的屏幕的分辨率、尺寸并不完全相同,而Android的布局管理器可以根据运行平台来调整组件的大小,而我们所需要做的就是选择合适的布局管理器。
与Swing编程不同的是,Android的布局管理器本身就是一个UI组件,所有的布局管理器都是ViewGroup的子类:
我们从上图也可以发现,所有布局都可作为容器类使用,因此可以调用多个重载的addView() 向布局管理器中添加组件,当然我们也可以用一个布局管理器嵌套其他布局管理器。
一、线性布局
线性布局是由LinearLayout 类来代表的,线性布局有点像AWT 编程里的FlowLayout ,它们都会将容器里的组件一个挨着一个排列起来,LinearLayout 不仅可以控制各组件横向排列,也可以控制纵向排列(android:orientation="vertical" 控制);
线性布局与AWT 中FlowLayout 的组大区别在于:Android 的线性布局不会换行,当组件一个挨着一个排到头了,剩下的组件将不会被显示出来。在Awt 中FlowLayout 则会另起一行排列多出来的组件。
LinearLayout 的常用XML 属性及相关方法
XML 属性 |
相关方法 |
说明 |
Android:gravity |
setGravity(int) |
设置布局管理器内组件对齐方式,该属性支持top 、buttom 、left 、center_vertical 、等等,可以同时指定多种对齐方式,多个属性值用竖线隔开,竖线前后不能有空格 |
android:orientation |
setOrientation(int) |
设置布局管理器内组件的排列方式,vertical: 垂直,默认horizontal: 水平 |
通过Activity代码的形式完成布局,此时就不需要xml配置文件了
package com.iflytek.activity; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class LayoutProjectActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); // setContentView(R.layout.linearlayout); LinearLayout linearLayout = new LinearLayout(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); linearLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); TextView textView = new TextView(this); textView.setLayoutParams(txtParams); textView.setText("xdwang"); textView.setTextSize(20); linearLayout.addView(textView); super.setContentView(linearLayout, layoutParams); } }
二、表格布局
表格布局由TableLayout 所代表,表格布局采用行列的形式管理UI 组件,TableLayout 并不需要明确的声明包含多少行、多少列,而是通过添加TableRow 、其他组件来控制表格的行数和列数。
每次向TableLayout 中添加一个TableRow ,该TableRow 就是一个表格行,TableRow 也是容器,因此它也可以不断地添加其他组件,每添加一个子组件该表格就增加一列。
如果直接向TableLayout 中添加组件,那么这个组件将直接占用一行。
在表格布局中,列的宽度由该列中最宽的那个单元格决定,整个表格布局的宽度则取决与父容器的宽度(默认总是占满父容器本身)
在表格布局管理器中,可以为单元格设置如下三种行为方式:
Shrinkable :如果某个列被设为Shrinkable ,那么该列的所有单元格的宽度可以被收缩,以保证表格能适应父容器的宽度。
Stretchable :如果某个列被设为Stretchable ,那么该列的所有单元格的宽度可以被拉伸,以保证组件能完全填满表格空余空间。
Collapsed :如果某个列被设为Collapsed ,那么该列的所有单元格会被隐藏;
TableLayout 继承了LinearLaout ,因此它完全可以支持LinearLayout 所支持的全部XML 属性,除此之外,TableLayout 还支持如下表所示的XML 属性。
Tab1eLayout 的常用XML 属性及相关方法:
XML 属性 |
相关方法 |
说明 |
android:collapseColumns |
setColumnCollapsed(int,boolean) |
设置需要被隐藏的列的列序号,多个列序号之间用逗号隔开 |
android:shrinkColumns |
setShrinkAllColumns(boolean) |
设置允许被收缩的列的列序号,多个列序号之间用逗号隔开 |
android:stretchColumns |
setStretchAllColumns(boolean) |
设置允许被拉伸的列的列序号,多个列序号之间用逗号隔开 |
tablelayout.xml(布局排列):
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableRow > <EditText 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="查询" /> </TableRow> <View android:layout_height="2px" android:background="#ccc"/> <!-- 分割线 --> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:text="请选择语言" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:checkedButton="@+id/rb1" android:orientation="vertical"> <RadioButton android:id="@+id/rb1" android:text="java"/> <RadioButton android:id="@+id/rb2" android:text="C#"/> </RadioGroup> </TableRow> </TableLayout>
Tablelayout2.xml(数据显示):
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:shrinkColumns="2" android:collapseColumns="0,1" android:background="@drawable/james"> <!--android:shrinkColumns="2",表示第3列为伸缩列 (因为下面的邮箱太长了,会将地址撑开,看不见的)--> <!--android:collapseColumns="0,1"设置不显示列,如果有更多的列,则使用,分割 --> <TableRow > <TextView android:layout_column="0" android:gravity="center_horizontal" android:padding="8px" android:text="ID" /> <!-- android:layout_column,这个没有提示,规定出了表格的列的编号 --> <!-- android:gravity="center_horizontal"表示居中显示 --> <TextView android:layout_column="1" android:gravity="center_horizontal" android:padding="8px" android:text="姓名" /> <TextView android:layout_column="2" android:gravity="center_horizontal" android:padding="8px" android:text="邮箱" /> <TextView android:layout_column="3" android:gravity="center_horizontal" android:padding="8px" android:text="地址" /> </TableRow> <View android:layout_height="2px" android:background="#ccc"/> <!-- 分割线 --> <TableRow > <TextView android:layout_column="0" android:gravity="center_horizontal" android:padding="8px" android:text="xdwang" /> <TextView android:layout_column="1" android:gravity="center_horizontal" android:padding="8px" android:text="王旭东" /> <TextView android:layout_column="2" android:gravity="center_horizontal" android:padding="8px" android:text="xdwangiflytek@gmail.com" /> <TextView android:layout_column="3" android:gravity="center_horizontal" android:padding="8px" android:text="安徽合肥" /> </TableRow> </TableLayout>
Activity动态生成
package com.iflytek.activity; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class LayoutProjectActivity extends Activity { private String titleData[][] = new String[][] { { "ID", "姓名", "邮箱", "地址" }, { "xdwang", "王旭东", "xdwangiflytek@gmail.com", "安徽合肥" }, { "xdwang2", "王旭东2", "xdwangiflytek2@gmail.com", "安徽合肥" } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TableLayout tableLayout = new TableLayout(this); TableLayout.LayoutParams tableParams= new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); tableLayout.setBackgroundResource(R.drawable.james); //设置表格行 for (int i = 0; i < this.titleData.length; i++) { TableRow tableRow = new TableRow(this); for (int j = 0; j <this.titleData[i].length; j++) { TextView textView = new TextView(this); textView.setText(this.titleData[i][j]); tableRow.addView(textView, j);//加入一个编号 } tableLayout.addView(tableRow);//向表格中增加若干表格行 } super.setContentView(tableLayout, tableParams); } }
通过程序代码实现的表格布局本身比较麻烦的,一般更多的情况下是使用配置文件的形式完成。
三、帧布局 (框架布局)
帧布局由FrameLayout 所代表,FrameLayout 直接继承了ViewCroup 组件。
帧布局容器为每个加入其中的组件创建一个空白的区域〔称为一帧) ,所有每个子组件占据一帧,这些帧都会根据gravity 属性执行自动对齐。也就是说,帧布局的效果有点类似于AWT 编程的CardLayout ,都是把组件一个一个地叠加在一起。与CardLayout 的区别在于,CardLayout 可以将下面的Card 移上来,但FrameLayout 则没有提供相应的方法.
FrameLayout 的常用XML 属性及相关方法
XML 属性 |
相关方法 |
说明 |
android:foreground |
setForeground(Drawable) |
设置该帧布局容器的前景图像 |
android:foregroundGravity |
setForegroundGravity(int) |
定义绘制前景图像的gravity 属性 |
android:measureAllChildren |
setMeasureAllChildren(boolean) |
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. |
Activity动态添加帧框架布局
package com.iflytek.activity; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; public class LayoutProjectActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout frameLayout = new FrameLayout(this); FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); FrameLayout.LayoutParams viewParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.james); EditText editText = new EditText(this); editText.setText("这里是内容"); Button button = new Button(this); button.setText("按钮"); frameLayout.addView(imageView, viewParams); frameLayout.addView(editText, viewParams); frameLayout.addView(button, viewParams); super.setContentView(frameLayout, frameParams); } }
四、相对布局
相对布局由 RelativeLayout 代表,相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的,因此这种布局方式被称为相对布局。
如果A 组件的位置是由B 组件的位置来决定的,Android 要求先定义B 组件,再定义A 组件。
RelativeLayout 的常用XML 属性及相关方法
XML 属性 |
相关方法 |
说明 |
android:gravity |
setGravity(int) |
设置该布局弃器内部各子组件的对齐方式 |
android:ignoreGravity |
setIgnoreGravity(int) |
设置哪个组件不受gravity 组件的影响 |
为了控制该布局容器中各子组件的布局分布,RelativeLayout 提供了一个内部类:
RelativeLayout.LayoutParams, 该类提供了大量的XML 属性来控制RelativeLayout 布局容器中子组件的布局分布。
relativelayout.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlId" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/james"/> <!-- 以组件id确定参考位置 --> <ImageView android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_toRightOf="@id/iv1"/> <!-- 放在第一张图片的右边 --> <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/iv2" android:layout_toRightOf="@id/iv1" android:text="@string/hello" /> </RelativeLayout>
Activity动态生成:
package com.iflytek.activity; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.EditText; import android.widget.RelativeLayout; public class LayoutProjectActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.relativelayout);// 要读取已经存在的布局管理器 RelativeLayout relativeLayout = (RelativeLayout) super .findViewById(R.id.rlId); RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); relativeParams.addRule(RelativeLayout.LEFT_OF, R.id.tv1); relativeParams.addRule(RelativeLayout.BELOW, R.id.iv1); EditText editText = new EditText(this); relativeLayout.addView(editText, relativeParams); } }
五、绝对布局
绝对布局由AbsoluteLayout 代表。是Android2.3.3版本之前的布局管理器,已废弃了。绝对布局就像Java AWT 编程中的空布局,就是Android 不提供任何布局控制,而是由开发人员自己通过X 坐标、Y 坐标来控制组件的位置。当使用AbsoluteLayout 作为布局容器时,布局容器不再管理子组件的位置、大小—这些都需要开发人员自己控制。
使用绝对布局时,每个子组件都可指定如下两个XML 属性。
layout_x :指定该子组件的X 坐标。
layout_y :指定该子组件的Y 坐标
布局管理器的嵌套
<?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" > <!-- 首先外面定义一个总的布局管理器,一般来说用的最多的还是线性和表格两种布局 --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查询" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- 注意这里的高度不能再全部铺满了,否则下面的布局都没空间了 --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/james"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> </LinearLayout> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TableRow> <EditText 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="这是什么呢" /> </TableRow> </TableLayout> </LinearLayout>
发表评论
-
This version of ADT requires android SDK
2013-07-25 16:45 1542Windows系统下用Eclipse开发工具开发An ... -
Android学习13-----网络通信(4) WebView组件
2012-11-27 09:18 2455WebView 是一个开发的浏览 ... -
Android学习13-----网络通信(3) 与Web Service进行通讯
2012-11-26 09:40 1859这里我们的WebService使用xFire开发。 ... -
Android学习13-----网络通信(2) 与Socket交换数据
2012-11-23 09:11 3268对于网络开发而言,最常用的交互模式:WebService、We ... -
Android学习13-----网络通信(1) 与WEB服务器交换数据
2012-11-22 09:11 2159与Web服务器交互: 如果手机要想与 web ... -
Android学习11-----多媒体技术(5) 媒体录制
2012-11-16 08:10 1848在Android中通过android.media ... -
Android学习11-----多媒体技术(4) 使用摄像头拍照,多点触控
2012-11-15 08:37 2835一、摄像头拍照 前面说媒体播放 时了解了 ... -
Android学习11-----多媒体技术(3) 媒体播放
2012-11-14 08:25 1382在 Androi ... -
Android学习11-----多媒体技术(2) Animation
2012-11-13 08:47 1959一、渐变动画, Tweened Animation ... -
Android学习11-----多媒体技术(1) 绘制简单图形,Bitmap,Matrix
2012-11-12 08:48 1569一、绘制简单图 ... -
Android学习12-----手机服务(4) 传感器
2012-11-19 09:13 1987传感器一般用于游戏中,在 Android 系统中为 ... -
Android学习12-----手机服务(1) 取得电池电量和声音服务:AudioManager
2012-11-18 11:18 3432一、取得电池电量信息 ... -
Android学习10-----Android组件通信 (8) 桌面显示组件:AppWidget
2012-11-02 08:36 2005一、 AppWidget 在使用 Androi ... -
Android学习10-----Android组件通信 (7) 广播机制:Broadcast
2012-11-01 08:43 1481一、 广播: 广播也是一种信息的发送机制,在 ... -
Android学习10-----Android组件通信 (6) PendingIntent
2012-10-31 08:20 2206Intent 的主要功能是表示用 ... -
Android学习10-----Android组件通信 (5) Service
2012-10-30 08:25 1680Service 基本组成: ... -
Android学习10-----Android组件通信 (4) 消息机制
2012-10-29 08:22 1516在 Android 操作系统中存在着消息队列的操作 ... -
Android学习10-----Android组件通信 (3) ActivityGroup
2012-10-26 08:23 2267导航栏在 Android 中的应用是很常见的,前面 ... -
Android学习10-----Android组件通信 (2) Activity生命周期
2012-10-25 08:16 1250Activity 是整个 Android 平台的基 ... -
Android学习10-----Android组件通信 (1) Intent
2012-10-24 08:43 1937在一个项目之中,会由多个 Activity ...
相关推荐
作为使用androidsdk构建这些应用程序的实用指南书籍,《android高级编程》从始至终穿插了一系列示例项目,每个项目都引入android的新功能和新技术,以助您达到最圆满的学习效果。书中介绍android的所有基本功能,并...
NULL 博文链接:https://sarin.iteye.com/blog/1754910
4. 响应式设计:Android 应用程序可以根据设备类型、屏幕尺寸等因素调整布局和显示方式,以适应不同的设备和用户需求。 5. 多媒体支持:Android 支持常见的音频、视频、图像等多媒体格式,可以实现各种多媒体应用。 ...
NULL 博文链接:https://sarin.iteye.com/blog/1757401
Android应用源码可以管理U盘的文件管理器 Android应用源码哔哩哔哩弹幕网站源码 Android应用源码基于bmob的即时聊天 Android应用源码大学生考勤项目源码 Android应用源码带密码登陆的密码保险箱 Android应用源码开关...
4. 响应式设计:Android 应用程序可以根据设备类型、屏幕尺寸等因素调整布局和显示方式,以适应不同的设备和用户需求。 5. 多媒体支持:Android 支持常见的音频、视频、图像等多媒体格式,可以实现各种多媒体应用。 ...
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
NULL 博文链接:https://sarin.iteye.com/blog/1756874
3> 学习使用ANDROID操作系统 4> 开发与运行(卸载)第一个ANDROID应用 5> 项目的目录结构 6> 项目清单文件分析 7> 分析第一个ANDROID应用的启动过程 8> 电话拔打 9> 查看手机模拟器往控制台输出的日志信息 10> 如何...
NULL 博文链接:https://sarin.iteye.com/blog/1756332
NULL 博文链接:https://sarin.iteye.com/blog/1694878
android实验界面设计:布局管理器.doc
内容概要:本次设计主要实现对学生信息的管理以及对页面布局的优化等,是对ListView、AlertDialog、ProgressDialog、Menu等重要对象的练习和使用,适合Android初学者学习。 ps:项目中用到的webservice接口来自于...
关于Android布局讲解对于想关于Android进一步学习的人,可那去借鉴。
自己在学习过程中总结的知识点 (3)Android体系结构和Android项目目录 Android是基于Linux内核的软件平台和操作系统,采用了软件堆栈的架构,共分为4 层:第一层是Linux内核,提供由操作系统内核管理的底层基础功能...
android实验3界面设计:布局管理器.doc
4. 响应式设计:Android 应用程序可以根据设备类型、屏幕尺寸等因素调整布局和显示方式,以适应不同的设备和用户需求。 5. 多媒体支持:Android 支持常见的音频、视频、图像等多媒体格式,可以实现各种多媒体应用。 ...
android实验3界面设计:布局管理器.doc
2-1 布局管理器 线性布局(LinearLayout) 相对布局(RelativeLayout) 2-1-1 LinearLayout 常用属性 android:id 相当于姓名 android:layout_width 宽度 android:layout_height 高度 Android:text 文字,如果是英文...