1、帧布局 FrameLayout:

是最简单的一个布局对象。在他里面的的所有显示对象爱你过都将固定在屏幕的左上角,不能指定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部分或全部挡住。

android:layout_width=”wrap_content” android:layout_height=”wrap_content”>

2、线性布局 LinearLayout:

线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。

3、绝对布局 AbsoluteLayout

绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。分辨率不一样的屏幕,显示的位置也会有所不同。

android:layout_y=”40dip”

4、相对布局 RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout android:id=”@+id/RelativeLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#fff”
xmlns:android=”http://schemas.android.com/apk/res/android”>

<ImageView android:id=”@+id/ImageView01″
android:src=”@drawable/android”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”40dip”
>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView01″
android:text=”Android2.2 学习指南”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_below=”@id/ImageView01″
android:layout_centerHorizontal=”true”

android:layout_marginTop=”10dip”>
</TextView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_below=”@id/TextView01″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”5dip
“>
</TextView>
</RelativeLayout>

让我们看一下在WQVGA的模拟器下的显示效果:

再看一下在更大屏幕(WVGA800)模拟器上的显示效果:

从上图可以看到界面效果基本保持了一致,而不是像绝对定位一样龟缩在左上角;同学们看到自动缩放的功能是采用了dip做单位带来的好处。

RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false
android: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,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离

5、表格布局 TableLayout

表格布局TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定。

<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="0"> <TableRow android:layout_width="fill_parent" android:layout_height="200dip"> <TextView android:textSize="18dip" android:layout_span="2" android:layout_gravity="center" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Color Test"> </TextView> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="20dip"> <TextView android:background="#ff00ff00" android:layout_width="fill_parent" android:layout_height="fill_parent"> </TextView> <TextView android:text="#ff00ff00" android:background="#000"> </TextView> </TableRow> </TableLayout>

Android中的常用布局_第1张图片

其中 android:stretchColumns=”0″ 作用是让第一列可以扩展到所有可用空间;下面我们讲一下TableLayout几个重要的属性:

collapseColumns – 设置隐藏那些列,列ID从0开始,多个列的话用”,”分隔
stretchColumns – 设置自动伸展那些列,列ID从0开始,多个列的话用”,”分隔
shrinkColumns -设置自动收缩那些列,列ID从0开始,多个列的话用”,”分隔

可以用”*”来表示所有列,同一列可以同时设置为shrinkable和stretchable。

Tip:TableRow也是一个Layout,里面的元素会水平排列,如果TableRow的父元素不是TableLayout的话,那么他会表现的像一个LinearLayout。

<?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:stretchColumns="1"> <TableRow> <TextView android:padding="5dip" android:text="打开..." android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:padding="5dip" android:layout_gravity="right" android:text="Ctrl+O" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> <TableRow> <TextView android:padding="5dip" android:text="保存..." android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:padding="5dip" android:layout_gravity="right" android:text="Ctrl+S" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> <View android:background="#fff" android:id="@+id/view1" android:layout_width="fill_content" android:layout_height="2dip"></View> <TableRow> <TextView android:padding="5dip" android:text="打开..." android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:padding="5dip" android:layout_gravity="right" android:text="Ctrl+O" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> <TableRow> <TextView android:padding="5dip" android:text="保存..." android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:padding="5dip" android:layout_gravity="right" android:text="Ctrl+S" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> </TableLayout>

更多相关文章

  1. 跟我学Android之六 布局
  2. 68.android 简单的布局展示不全的问题,明明写的是android:layout_
  3. Android 中LayoutInflater(布局加载器)源码篇之parseInclude方法
  4. Android 中LayoutInflater(布局加载器)源码篇之rInflate方法
  5. Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG
  6. 【android】初学安卓,简单布局和activity切换
  7. 布局(一)
  8. 浅谈Android五大布局——LinearLayout、FrameLayout, AbsoulteLa
  9. Android 布局 精准定位 平衡定位 相对定位

随机推荐

  1. android google map v1 v2 v3 参考
  2. Android(安卓)录音数据传输
  3. android framework源码编译流程
  4. java传感器应用之指南针
  5. Android手机控制流量那几招
  6. Android(安卓)自带email应用对html支持不
  7. Android(安卓)MVP开发模式实践
  8. Android(安卓)选项菜单Menu
  9. Android远程服务例程
  10. adb如何打印kernel输出log