- 注册时间
 - 2011-3-22
 - 最后登录
 - 2013-6-27
 - 在线时间
 - 11644 小时
 - 阅读权限
 - 150
 - 积分
 - 62779
 - 帖子
 - 28923
 - 精华
 - 1
 - UID
 - 6
  
 
 
 
   
 | 
上次创建了项目,了解了项目结构(说的不是很清楚啊)。 
这次我们主要学习代码编写。忘了上次的hello代码没上传。 
项目创建好后系统会自动生成个res/layout/mian.xml文件。 这个mian.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:stretchColumns="1">
 
 -         <TextView android:id="@+id/txtViewMessage" 
 
 -                 android:textColor="@color/loginMessage" android:layout_height="50dp" android:text="@string/hello"
 
 -                 android:layout_width="match_parent">
 
 -         </TextView>
 
 -         <TableRow>
 
 -                 <TextView android:text="用户名:" android:textStyle="bold"
 
 -                         android:gravity="right" android:padding="3dip" />
 
  
-                 <EditText android:id="@+id/txtUserName" android:padding="3dip"
 
 -                         android:scrollHorizontally="true" />
 
 -         </TableRow>
 
  
-         <TableRow>
 
 -                 <TextView android:textStyle="bold" android:gravity="right"
 
 -                         android:padding="3dip" android:text="密    码:" />
 
  
-                 <EditText android:id="@+id/txtPassword" android:password="true"
 
 -                         android:padding="3dip" android:scrollHorizontally="true" />
 
 -         </TableRow>
 
  
-         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
 -                 android:layout_width="fill_parent" android:layout_height="wrap_content"
 
 -                 android:padding="10dip">
 
  
 
-                 <!-- 确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平 -->
 
 -                 <Button android:id="@+id/btnOK" android:layout_height="wrap_content"
 
 -                         android:text="确  定" android:layout_width="150dp" />
 
 -                 <!-- 取消按钮和容器的右边齐平,并且设置左边的边距为10dip -->
 
 -                 <Button android:id="@+id/btnCancel" android:layout_height="wrap_content"
 
 -                         android:layout_alignParentRight="true" android:layout_marginLeft="10dip"
 
 -                         android:text="取  消" android:layout_width="150dp" />
 
  
-         </RelativeLayout>
 
 - </TableLayout>
 
  复制代码 这里和html不同的是他多有值都是从values/下面读取的。就是前面介绍的R.java文件。这里不再详细说明 
android 布局有4中,分别是TableLayout、RelativeLayout 、LinearLayout、FrameLayout  一个布局容器可以嵌套多个布局。- LinearLayout - 线形布局。 
 
 -   orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列
 
  
-   gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等
 
  
-   FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示
 
  
-   TableLayout - 表格式布局。
 
  
-   TableRow - 表格内的行,行内每一个元素算作一列
 
  
-   collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开
 
  
-   stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开
 
  
-   shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开
 
  
-   RelativeLayout - 相对定位布局。
 
  
-   layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)
 
  
-   layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离
 
  
-   layout_below - 放置当前元素到指定的元素的下面
 
  
-   layout_alignRight - 当前元素与指定的元素右对齐
 
  
-   layout_width - 宽。
 
  
-   fill_parent: 宽度跟着父元素走;
 
  
-   wrap_content: 宽度跟着本身的内容走;
 
  
-   直接指定一个 px 值来设置宽
 
  
-   layout_height - 高。
 
  
-   fill_parent: 高度跟着父元素走;
 
  
-   wrap_content: 高度跟着本身的内容走;
 
  
-   直接指定一个 px 值来设置高
 
  
-   < ?xml version="1.0" encoding="utf-8"?>
 
  
-   < !--
 
  
-   layout_width - 宽。fill_parent: 宽度跟着父元素走;wrap_content: 宽度跟着本身的内容走;直接指定一个 px 值来设置宽
 
  
-   layout_height - 高。fill_parent: 高度跟着父元素走;wrap_content: 高度跟着本身的内容走;直接指定一个 px 值来设置高
 
  
-   -->
 
  
-   < !--
 
  
-   LinearLayout - 线形布局。
 
  
-   orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列
 
 - gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,详见文档 
 
 -   -->
 
  
-   < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
  
-   android:orientation="vertical" android:gravity="right"
 
  
-   android:layout_width="fill_parent" android:layout_height="fill_parent">
 
  
-   < !--
 
  
-   FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示
 
  
-   -->
 
  
-   < FrameLayout android:layout_height="wrap_content"
 
  
-   android:layout_width="fill_parent">
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="FrameLayout">
 
  
-   < /TextView>
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="Frame Layout">
 
  
-   < /TextView>
 
  
-   < /FrameLayout>
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="@string/hello" />
 
  
-   < !--
 
  
-   TableLayout - 表格式布局。
 
  
-   TableRow - 表格内的行,行内每一个元素算作一列
 
  
-   collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开
 
  
-   stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开
 
  
-   shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开
 
  
-   -->
 
  
-   < TableLayout android:id="@+id/TableLayout01"
 
  
-   android:layout_width="fill_parent" android:layout_height="wrap_content"
 
  
-   android:collapseColumns="1">
 
  
-   < TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
 
  
-   android:layout_height="wrap_content">
 
  
-   < TextView android:layout_width="wrap_content"
 
 - android:layout_weight="1" android:layout_height="wrap_content" 
 
 -   android:text="行1列1" />
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_weight="1" android:layout_height="wrap_content"
 
  
-   android:text="行1列2" />
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_weight="1" android:layout_height="wrap_content"
 
  
-   android:text="行1列3" />
 
  
-   < /TableRow>
 
  
-   < TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content">
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="行2列1" />
 
  
-   < /TableRow>
 
  
-   < /TableLayout>
 
  
-   < !--
 
  
-   AbsoluteLayout - 绝对定位布局。
 
  
-   layout_x - x 坐标。以左上角为顶点
 
  
-   layout_y - y 坐标。以左上角为顶点
 
  
-   -->
 
  
-   < AbsoluteLayout android:layout_height="wrap_content"
 
  
-   android:layout_width="fill_parent">
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="AbsoluteLayout"
 
  
-   android:layout_x="100px"
 
  
-   android:layout_y="100px" />
 
  
-   < /AbsoluteLayout>
 
  
-   < !--
 
  
-   RelativeLayout - 相对定位布局。
 
  
-   layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)
 
  
-   layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离
 
  
-   layout_below - 放置当前元素到指定的元素的下面
 
  
-   layout_alignRight - 当前元素与指定的元素右对齐
 
  
-   -->
 
  
-   < RelativeLayout android:id="@+id/RelativeLayout01"
 
  
-   android:layout_width="fill_parent" android:layout_height="fill_parent">
 
  
-   < TextView android:layout_width="wrap_content" android:id="@+id/abc"
 
 - android:layout_height="wrap_content" android:text="centerInParent=true" 
 
 -   android:layout_centerInParent="true" />
 
  
-   < TextView android:layout_width="wrap_content"
 
  
-   android:layout_height="wrap_content" android:text="marginLeft=20px"
 
  
-   android:layout_marginLeft="20px" />
 
  
-   < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xxx" android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
 
  
-   < /RelativeLayout>
 
  
-   < /LinearLayout>
 
  复制代码 |   
 
- 
1
查看全部评分 
 
- 
 
 
  
 
 |