android xml布局总结篇

【android-tips】android xml布局总结篇

一.背景

        可能很多人跟我一样,做了好久的android程序,却一直没有认真地坐下来好好学习下xml文件的布局。其实有的时候我们用view绘制或是利用ADT的图形界面功能就可以轻松搞定布局,但是最好还是静下来学习下xml的布局文件具体写法。这一节我们要绘制如下图所示的界面。

二基础知识

       首先我们要了解android到底有那些布局,和每个布局类型的区别。

 1.线性布局 LinearLayout

         线性布局分两种。一种是水平布局,一种是垂直布局。下面我们根据上图举例子。

         先把上图的代码贴出来吧!

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

  2. <LinearLayout android:orientation="vertical"

  3. android:layout_width="fill_parent" android:layout_height="fill_parent"

  4. xmlns:android="http://schemas.android.com/apk/res/android">

  5. <TextView android:layout_height="wrap_content" android:text="@string/note_title"

  6. android:layout_width="wrap_content" android:padding="10dp"></TextView>

  7. <LinearLayout android:layout_height="fill_parent"

  8. android:layout_width="fill_parent" android:layout_weight="1">

  9. <EditText android:id="@+id/EditText02" android:layout_width="fill_parent"

  10. android:layout_height="fill_parent" android:gravity="left"

  11. android:hint="@string/edithint"></EditText>

  12. </LinearLayout>

  13. <LinearLayout android:layout_height="fill_parent"

  14. android:layout_width="fill_parent" android:layout_weight="2"

  15. android:gravity="center"

android:orientation="horizontal"

> <ImageButton android:id="@+id/ImageButton01" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_003" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton02" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_004" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton03" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_007" android:layout_margin="3dp"></ImageButton> <ImageButton android:id="@+id/ImageButton04" android:layout_width="72dp" android:layout_height="72dp" android:src="@drawable/sketchy_paper_011" android:layout_margin="3dp"></ImageButton> </LinearLayout> </LinearLayout> 
可以看到,上图是由三部分组成。在大的LinearLayout从上而下垂直分布着三个内容:TextView,LinearLayout,LinearLayout。所以总体的LinearLayout是垂直布局

android:orientation="vertical"

下面我们来看水平布局

其实就是上图中的最下面那个LinearLayout。四个图标平行排列。

android:orientation="horizontal"

2.相对布局 RelativeLayout

这个布局相对简单一点。一般来讲利用ADT自己拖放按钮就可以。基本上可以随意布局。如下图所示

代码是

 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:paddingBottom="@dimen/activity_vertical_margin"

  6. android:paddingLeft="@dimen/activity_horizontal_margin"

  7. android:paddingRight="@dimen/activity_horizontal_margin"

  8. android:paddingTop="@dimen/activity_vertical_margin"

  9. tools:context=".MainActivity" >

  10.  
  11. <Button

  12. android:id="@+id/button1"

  13. style="?android:attr/buttonStyleSmall"

  14. android:layout_width="wrap_content"

  15. android:layout_height="wrap_content"

  16. android:layout_alignParentLeft="true"

  17. android:layout_alignParentTop="true"

  18. android:text="Button" />

  19.  
  20. <Button

  21. android:id="@+id/button2"

  22. style="?android:attr/buttonStyleSmall"

  23. android:layout_width="wrap_content"

  24. android:layout_height="wrap_content"

  25. android:layout_alignBaseline="@+id/button1"

  26. android:layout_alignBottom="@+id/button1"

  27. android:layout_alignParentRight="true"

  28. android:layout_marginRight="14dp"

  29. android:text="Button" />

  30.  
  31. <Button

  32. android:id="@+id/button3"

  33. style="?android:attr/buttonStyleSmall"

  34. android:layout_width="wrap_content"

  35. android:layout_height="wrap_content"

  36. android:layout_below="@+id/button1"

  37. android:layout_centerHorizontal="true"

  38. android:layout_marginTop="97dp"

  39. android:text="Button" />

  40.  
  41. <Button

  42. android:id="@+id/button4"

  43. style="?android:attr/buttonStyleSmall"

  44. android:layout_width="wrap_content"

  45. android:layout_height="wrap_content"

  46. android:layout_alignLeft="@+id/button1"

  47. android:layout_below="@+id/button3"

  48. android:layout_marginTop="89dp"

  49. android:text="Button" />

  50.  
  51. <Button

  52. android:id="@+id/button5"

  53. style="?android:attr/buttonStyleSmall"

  54. android:layout_width="wrap_content"

  55. android:layout_height="wrap_content"

  56. android:layout_alignLeft="@+id/button2"

  57. android:layout_alignTop="@+id/button4"

  58. android:text="Button" />

  59.  
  60. </RelativeLayout>

layout_marginBottom是指控件边以外空下的距离,比如Button1和Button2垂直显示,将Button1中layout_marginBottom = 10dp,那么Button1与Button2之间将有10dp距离。

下面这两句是居左显示和居右显示

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

常用语句总结:

 android:layout_toLeftOf —— 该组件位于引用组件的左方
 

    android:layout_toRightOf —— 该组件位于引用组件的右方
 

    android:layout_above —— 该组件位于引用组件的上方
 

    android:layout_below —— 该组件位于引用组件的下方
 

       android:layout_alignParentLeft —— 该组件是否对齐父组件的左端
 

       android:layout_alignParentRight —— 该组件是否齐其父组件的右端
 

       android:layout_alignParentTop —— 该组件是否对齐父组件的顶部
 

       android:layout_alignParentBottom —— 该组件是否对齐父组件的底部
 

    android:layout_centerInParent —— 该组件是否相对于父组件居中
 

    android:layout_centerHorizontal —— 该组件是否横向居中
   

  android:layout_centerVertical —— 该组件是否垂直居中

总之,相对视图应该是最有用的,具体的操作比较复杂,更多的是通过图形界面拖拉,再用代码微调!

3.FrameLayout

这个布局很简单,而且感觉有点二二的,哈哈!就是控件一个挨一个在左上角罗列。

代码

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

  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent" >

  5.  
  6. <Button

  7. android:id="@+id/button1"

  8. style="?android:attr/buttonStyleSmall"

  9. android:layout_width="wrap_content"

  10. android:layout_height="wrap_content"

  11. android:text="Button" />

  12.  
  13. <Button

  14. android:id="@+id/button2"

  15. android:layout_width="126dp"

  16. android:layout_height="135dp"

  17. android:text="Button" />

  18.  
  19. <Button

  20. android:id="@+id/button3"

  21. android:layout_width="194dp"

  22. android:layout_height="232dp"

  23. android:text="Button" />

  24.  
  25. </FrameLayout>

4.绝对布局 AbsoluteLayout

绝对布局比较容易使用,就是以左上方为原点建立坐标系。每个控件用layout_x和layout_y表示位置。但是据说这种布局比较刚性,不容易适配各种终端,所以要慎用!

代码

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

  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent" >

  5.  
  6. <Button

  7. android:id="@+id/button1"

  8. android:layout_width="wrap_content"

  9. android:layout_height="wrap_content"

  10. android:layout_x="44dp"

  11. android:layout_y="18dp"

  12. android:text="Button" />

  13.  
  14. <Button

  15. android:id="@+id/button2"

  16. android:layout_width="wrap_content"

  17. android:layout_height="wrap_content"

  18. android:layout_x="122dp"

  19. android:layout_y="173dp"

  20. android:text="Button" />

  21.  
  22. <Button

  23. android:id="@+id/button3"

  24. android:layout_width="wrap_content"

  25. android:layout_height="wrap_content"

  26. android:layout_x="36dp"

  27. android:layout_y="133dp"

  28. android:text="Button" />

  29.  
  30. </AbsoluteLayout>

5.表格布局 TableLayout

TableLayout有点像一个表格或是矩阵。在布局中加入TableRow,它的属性是horizontal所以每个TableRow只能横放。它里面的每个控件的高都是一样的。下图所示,是加入了一个TableRow和里面的控件。

代码

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

  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent" >

  5.  
  6. <TableRow

  7. android:id="@+id/tableRow1"

  8. android:layout_width="wrap_content"

  9. android:layout_height="wrap_content" >

  10.  
  11. <Button

  12. android:id="@+id/button1"

  13. style="?android:attr/buttonStyleSmall"

  14. android:layout_width="wrap_content"

  15. android:layout_height="wrap_content"

  16. android:text="Button" />

  17.  
  18. <Button

  19. android:id="@+id/button2"

  20. style="?android:attr/buttonStyleSmall"

  21. android:layout_width="wrap_content"

  22. android:layout_height="wrap_content"

  23. android:text="Button" />

  24.  
  25. <Button

  26. android:id="@+id/button3"

  27. style="?android:attr/buttonStyleSmall"

  28. android:layout_width="wrap_content"

  29. android:layout_height="wrap_content"

  30. android:text="Button" />

  31.  
  32. <Button

  33. android:id="@+id/button4"

  34. style="?android:attr/buttonStyleSmall"

  35. android:layout_width="wrap_content"

  36. android:layout_height="wrap_content"

  37. android:text="Button" />

  38.  
  39. <TextView

  40. android:id="@+id/textView1"

  41. android:layout_width="wrap_content"

  42. android:layout_height="wrap_content"

  43. android:text="TextView" />

  44.  
  45. </TableRow>

  46.  
  47. </TableLayout>

  ok!你学会了么,have fun!

猜你喜欢

转载自blog.csdn.net/kingmax54212008/article/details/82877118