Android学习06-----布局管理器
为了更好的管理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>