blog | 业余项目 | 关于Karry | RSS订阅 | 我在阅读 | 管理

Android 学习笔记(1)–布局方式

今天终于收到了《Google Android SDK 开发范例大全》,迅速的配置好了开发环境,写了一个hello world。

发现这本书完全是围绕示例来写的,完全没有系统的总结,我就把学到的内容加上网上的搜索把一些知识点总结一下,记录再这里,方便自己以后开发的时候查询。

Android 的layout是通过xml配置的(这让我想起了Flex中的界面),而且也有样式表(不是css,也是一个xml)。

一段布局代码:

  1. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android
  2. android:orientation=”vertical”   android:layout_width=”fill_parent”
  3. android:layout_width = “fill_parent”
  4. android:layout_height=”fill_parent”>
  5. <TextView android:layout_width=”full_parent”  //宽度与父容器一样(也就是和屏幕宽度一样)
  6. android:layout_heigth =”wrap_content” //高度自适应
  7. android:text=”hello world!”
  8. />
  9. </LinearLayout>

Android 的布局类型有五种:

  • FrameLayout:框架布局,这个布局浏览单张图片的时候很合适,放入其中的所有元素都被放置在FrameLayout区域最左上的区域,而且无法为这些元素指定一个确切的位置。如果一个FrameLayout里有多个子元素,那么后边的子元素的显示会重叠在前一个元素上。这个布局方式的宽与高的参数 一般是用wrap_content。
  • LinearLayout:线性布局管理器,只能进行单行布局,是由参数android:orientation(方位)决定的,分为水平和垂直两种(或者说是行 与 列两种),vertical表示竖直排列,horizontal表示横向排列。
  • TableLayout:任意行和列的表格布局管理器。其中TableRow代表一行,TableRow的每一个视图组件代表一个单元格。
  • AbsoluteLayout:绝对布局管理器,坐标轴的方式,这个很好理解,左上角是(0,0)点,往右x轴递增,往下Y轴递增,组件定位属性为android:layout_x 和 android:layout_y来确定坐标。
  • RelativeLayout:相对布局管理器,根据另外一个组件或是顶层父组件来确定下一个组件的位置。

再看一个采用TableLayout布局的例子(3行2列):

  1. <TableLayout xmlns:android=”http://schemas.android.com/apk/res/android
  2. android:layout_width=”fill_parent” android:layout_height=”fill_parent”
  3. android:stretchColumns=”1″>
  4. <TableRow>
  5. <TextView android:text=”用户名:” android:textStyle=”bold”  android:gravity=”right” android:padding=”3dip” />
  6. <EditText android:id=”@+id/username” android:padding=”3dip” android:scrollHorizontally=”true” />
  7. </TableRow>
  8. <TableRow>
  9. <TextView android:text=”登录密码:” android:textStyle=”bold” android:gravity=”right” android:padding=”3dip” />
  10. <EditText android:id=”@+id/password” android:password=”true” android:padding=”3dip” android:scrollHorizontally=”true” />
  11. </TableRow>
  12. <TableRow android:gravity=”right”>
  13. <Button android:id=”@+id/cancel” android:text=”取消” />
  14. <Button android:id=”@+id/login” android:text=”登录”  android:layout_width=”wrap_content”  android:layout_height=”wrap_content”/>
  15. </TableRow>
  16. </TableLayout>

Tags: ,

One Response to “Android 学习笔记(1)–布局方式”

  1. ztoomoo Says:

    你转做android 开发了?

Leave a Reply