设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1956|回复: 0

Android开发(三--布局文件) [复制链接]

Rank: 8Rank: 8

那个谁 发表于 2011-5-13 09:19:00 |显示全部楼层
上次创建了项目,了解了项目结构(说的不是很清楚啊)。
这次我们主要学习代码编写。忘了上次的hello代码没上传。
项目创建好后系统会自动生成个res/layout/mian.xml文件。 这个mian.xml就是个页面(布局文件)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.         android:stretchColumns="1">
  5.         <TextView android:id="@+id/txtViewMessage"
  6.                 android:textColor="@color/loginMessage" android:layout_height="50dp" android:text="@string/hello"
  7.                 android:layout_width="match_parent">
  8.         </TextView>
  9.         <TableRow>
  10.                 <TextView android:text="用户名:" android:textStyle="bold"
  11.                         android:gravity="right" android:padding="3dip" />

  12.                 <EditText android:id="@+id/txtUserName" android:padding="3dip"
  13.                         android:scrollHorizontally="true" />
  14.         </TableRow>

  15.         <TableRow>
  16.                 <TextView android:textStyle="bold" android:gravity="right"
  17.                         android:padding="3dip" android:text="密    码:" />

  18.                 <EditText android:id="@+id/txtPassword" android:password="true"
  19.                         android:padding="3dip" android:scrollHorizontally="true" />
  20.         </TableRow>

  21.         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  22.                 android:layout_width="fill_parent" android:layout_height="wrap_content"
  23.                 android:padding="10dip">


  24.                 <!-- 确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平 -->
  25.                 <Button android:id="@+id/btnOK" android:layout_height="wrap_content"
  26.                         android:text="确  定" android:layout_width="150dp" />
  27.                 <!-- 取消按钮和容器的右边齐平,并且设置左边的边距为10dip -->
  28.                 <Button android:id="@+id/btnCancel" android:layout_height="wrap_content"
  29.                         android:layout_alignParentRight="true" android:layout_marginLeft="10dip"
  30.                         android:text="取  消" android:layout_width="150dp" />

  31.         </RelativeLayout>
  32. </TableLayout>
复制代码
这里和html不同的是他多有值都是从values/下面读取的。就是前面介绍的R.java文件。这里不再详细说明
android 布局有4中,分别是TableLayout、RelativeLayout 、LinearLayout、FrameLayout  一个布局容器可以嵌套多个布局。
  1. LinearLayout - 线形布局。
  2.   orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列

  3.   gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等

  4.   FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示

  5.   TableLayout - 表格式布局。

  6.   TableRow - 表格内的行,行内每一个元素算作一列

  7.   collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开

  8.   stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开

  9.   shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开

  10.   RelativeLayout - 相对定位布局。

  11.   layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)

  12.   layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离

  13.   layout_below - 放置当前元素到指定的元素的下面

  14.   layout_alignRight - 当前元素与指定的元素右对齐

  15.   layout_width - 宽。

  16.   fill_parent: 宽度跟着父元素走;

  17.   wrap_content: 宽度跟着本身的内容走;

  18.   直接指定一个 px 值来设置宽

  19.   layout_height - 高。

  20.   fill_parent: 高度跟着父元素走;

  21.   wrap_content: 高度跟着本身的内容走;

  22.   直接指定一个 px 值来设置高

  23.   < ?xml version="1.0" encoding="utf-8"?>

  24.   < !--

  25.   layout_width - 宽。fill_parent: 宽度跟着父元素走;wrap_content: 宽度跟着本身的内容走;直接指定一个 px 值来设置宽

  26.   layout_height - 高。fill_parent: 高度跟着父元素走;wrap_content: 高度跟着本身的内容走;直接指定一个 px 值来设置高

  27.   -->

  28.   < !--

  29.   LinearLayout - 线形布局。

  30.   orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列
  31. gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,详见文档
  32.   -->

  33.   < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  34.   android:orientation="vertical" android:gravity="right"

  35.   android:layout_width="fill_parent" android:layout_height="fill_parent">

  36.   < !--

  37.   FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示

  38.   -->

  39.   < FrameLayout android:layout_height="wrap_content"

  40.   android:layout_width="fill_parent">

  41.   < TextView android:layout_width="wrap_content"

  42.   android:layout_height="wrap_content" android:text="FrameLayout">

  43.   < /TextView>

  44.   < TextView android:layout_width="wrap_content"

  45.   android:layout_height="wrap_content" android:text="Frame Layout">

  46.   < /TextView>

  47.   < /FrameLayout>

  48.   < TextView android:layout_width="wrap_content"

  49.   android:layout_height="wrap_content" android:text="@string/hello" />

  50.   < !--

  51.   TableLayout - 表格式布局。

  52.   TableRow - 表格内的行,行内每一个元素算作一列

  53.   collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开

  54.   stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开

  55.   shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开

  56.   -->

  57.   < TableLayout android:id="@+id/TableLayout01"

  58.   android:layout_width="fill_parent" android:layout_height="wrap_content"

  59.   android:collapseColumns="1">

  60.   < TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"

  61.   android:layout_height="wrap_content">

  62.   < TextView android:layout_width="wrap_content"
  63. android:layout_weight="1" android:layout_height="wrap_content"
  64.   android:text="行1列1" />

  65.   < TextView android:layout_width="wrap_content"

  66.   android:layout_weight="1" android:layout_height="wrap_content"

  67.   android:text="行1列2" />

  68.   < TextView android:layout_width="wrap_content"

  69.   android:layout_weight="1" android:layout_height="wrap_content"

  70.   android:text="行1列3" />

  71.   < /TableRow>

  72.   < TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"

  73.   android:layout_height="wrap_content">

  74.   < TextView android:layout_width="wrap_content"

  75.   android:layout_height="wrap_content" android:text="行2列1" />

  76.   < /TableRow>

  77.   < /TableLayout>

  78.   < !--

  79.   AbsoluteLayout - 绝对定位布局。

  80.   layout_x - x 坐标。以左上角为顶点

  81.   layout_y - y 坐标。以左上角为顶点

  82.   -->

  83.   < AbsoluteLayout android:layout_height="wrap_content"

  84.   android:layout_width="fill_parent">

  85.   < TextView android:layout_width="wrap_content"

  86.   android:layout_height="wrap_content" android:text="AbsoluteLayout"

  87.   android:layout_x="100px"

  88.   android:layout_y="100px" />

  89.   < /AbsoluteLayout>

  90.   < !--

  91.   RelativeLayout - 相对定位布局。

  92.   layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)

  93.   layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离

  94.   layout_below - 放置当前元素到指定的元素的下面

  95.   layout_alignRight - 当前元素与指定的元素右对齐

  96.   -->

  97.   < RelativeLayout android:id="@+id/RelativeLayout01"

  98.   android:layout_width="fill_parent" android:layout_height="fill_parent">

  99.   < TextView android:layout_width="wrap_content" android:id="@+id/abc"
  100. android:layout_height="wrap_content" android:text="centerInParent=true"
  101.   android:layout_centerInParent="true" />

  102.   < TextView android:layout_width="wrap_content"

  103.   android:layout_height="wrap_content" android:text="marginLeft=20px"

  104.   android:layout_marginLeft="20px" />

  105.   < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xxx" android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />

  106.   < /RelativeLayout>

  107.   < /LinearLayout>
复制代码
1

查看全部评分

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-3-29 18:37 , Processed in 1.094098 second(s), 15 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部