Chinaunix首页 | 论坛 | 博客
  • 博客访问: 531070
  • 博文数量: 119
  • 博客积分: 3167
  • 博客等级: 中校
  • 技术积分: 1215
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-20 21:21
文章分类

全部博文(119)

文章存档

2015年(21)

2012年(4)

2011年(1)

2007年(11)

2006年(50)

2005年(32)

分类: Android平台

2015-10-27 23:10:28

Activity布局初步

1. LinearLayout

主要属性android:orientation="vertical" 或 "horizontal", 指定该layout下的控件垂直方向还是水平方向


        android:id  —— 为控件指定相应的ID
        android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
        android:gravity —— 指定控件的基本位置,比如说居中,居右等位置
        android:textSize —— 指定控件当中字体的大小
        android:background —— 指定该控件所使用的背景色,RGB命名法
        android:width —— 指定控件的宽度
        android:height —— 指定控件的高度
        android:padding* —— 指定控件的内边距,也就是说控件当中的内容
        android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示

点击(此处)折叠或打开

  1. 例子
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android=""
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     >
  8.     <TextView
  9.         android:id="@+id/firstText"
  10.         android:text="第一行"
  11.         android:gravity="center_vertical"
  12.         android:textSize="15pt"
  13.         android:background="#aa0000"
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="wrap_content"
  16.         android:layout_weight="10000"
  17.         android:singleLine="true"/>
  18.     <TextView
  19.         android:id="@+id/secondText"
  20.         android:text="第二行"
  21.         android:gravity="center_vertical"
  22.         android:textSize="15pt"
  23.         android:background="#0000aa"
  24.         android:layout_width="fill_parent"
  25.         android:layout_height="wrap_content"
  26.         android:layout_weight="1"/>
  27. </LinearLayout>




2   TableLayout

    android:stretchColumns="0"   指定某一列的宽度自适应扩展

例子

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:stretchColumns="0">
  6.     <TableRow>
  7.         <TextView
  8.             android:text="@string/row1_column1"
  9.             android:background="#aa0000"
  10.             android:padding="3dip" />
  11.         <TextView
  12.             android:text="@string/row1_column1"
  13.             android:padding="3dip"
  14.             android:gravity="center_horizontal"
  15.             android:background="#00aa00"
  16.             ></TextView>
  17.         <TextView
  18.             android:text="@string/row1_column2"
  19.             android:gravity="right"
  20.             android:background="#0000aa"
  21.             android:padding="3dip" />
  22.     </TableRow>

  23.     <TableRow>
  24.         <TextView
  25.             android:text="@string/row2_column1"
  26.             android:padding="3dip" />
  27.         <TextView
  28.             android:text="@string/row2_column2"
  29.             android:gravity="right"
  30.             android:padding="3dip" />
  31.     </TableRow>
  32. </TableLayout>


3. LinearLayout 之间嵌套

主要属性, android:layout_weight="1"  用来指定该布局占总空间的几分之几
比如, 两个LinearLayout 都是 android:layout_weight="1" , 那么这两个layout就各占1/2
下面例子是LinearLayout和TableLayout嵌套


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <LinearLayout
  6.         android:orientation="horizontal"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         android:layout_weight="1">
  10.         <TextView
  11.             android:text="red"
  12.             android:gravity="center_horizontal"
  13.             android:background="#aa0000"
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="fill_parent"
  16.             android:layout_weight="1" />
  17.         <TextView
  18.             android:text="green"
  19.             android:gravity="center_horizontal"
  20.             android:background="#00aa00"
  21.             android:layout_width="wrap_content"
  22.             android:layout_height="fill_parent"
  23.             android:layout_weight="1" />
  24.         <TextView
  25.             android:text="blue"
  26.             android:gravity="center_horizontal"
  27.             android:background="#0000aa"
  28.             android:layout_width="wrap_content"
  29.             android:layout_height="fill_parent"
  30.             android:layout_weight="1" />
  31.         <TextView
  32.             android:text="yellow"
  33.             android:gravity="center_horizontal"
  34.             android:background="#aaaa00"
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="fill_parent"
  37.             android:layout_weight="1" />
  38.     </LinearLayout>


  39.     <LinearLayout
  40.         android:orientation="horizontal"
  41.         android:layout_width="fill_parent"
  42.         android:layout_height="fill_parent"
  43.         android:layout_weight="1">
  44.         
  45.         <TableLayout
  46.             android:layout_width="fill_parent"
  47.             android:layout_height="fill_parent"
  48.             android:stretchColumns="0">
  49.             <TableRow>
  50.                 <TextView
  51.                     android:text="@string/row1_column1"
  52.                     android:padding="3dip" />
  53.                 <TextView
  54.                     android:text="@string/row1_column1"
  55.                     android:padding="3dip"
  56.                     android:gravity="center_horizontal">
  57.                     </TextView>
  58.                 <TextView
  59.                     android:text="@string/row1_column2"
  60.                     android:gravity="right"
  61.                     android:padding="3dip" />
  62.             </TableRow>

  63.             <TableRow>
  64.                 <TextView
  65.                     android:text="@string/row2_column1"
  66.                     android:padding="3dip" />
  67.                 <TextView
  68.                     android:text="@string/row2_column2"
  69.                     android:gravity="right"
  70.                     android:padding="3dip" />
  71.             </TableRow>
  72.         </TableLayout>
  73.     </LinearLayout>
  74. </LinearLayout>


4. RelativeLayout

        android:layout_marginLeft="10px"  指定该控件和相邻控件左边距, 同理还有layout_marginRight等等

        android:layout_above 将该控件的底部至于给定ID的控件之上
        android:layout_below 将该控件的顶部至于给定ID的控件之下
        android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
        android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐

        android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
        android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
        android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
        android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
        android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐


        android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
        android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
        android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
        android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐

        android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
        android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
        android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央

点击(此处)折叠或打开

  1. // 相对布局例子

  2. <RelativeLayout xmlns:android=""
  3.                 android:layout_width="fill_parent"
  4.                 android:layout_height="wrap_content"
  5.                 android:padding="10px" >

  6.     <TextView android:id="@+id/label"
  7.               android:layout_width="fill_parent"
  8.               android:layout_height="wrap_content"
  9.               android:text="Type here:" />

  10.     <EditText android:id="@+id/entry"
  11.               android:layout_width="fill_parent"
  12.               android:layout_height="wrap_content"
  13.               android:background="@android:drawable/editbox_background"
  14.               android:layout_below="@id/label" />
  15.  
  16.     <Button android:id="@+id/ok"
  17.             android:layout_width="wrap_content"
  18.             android:layout_height="wrap_content"
  19.             android:layout_below="@id/entry"
  20.             android:layout_alignParentRight="true"
  21.             android:layout_marginLeft="10px"
  22.             android:text="OK" />

  23.     <Button android:layout_width="wrap_content"
  24.             android:layout_height="wrap_content"
  25.             android:layout_toLeftOf="@id/ok"
  26.             android:layout_alignTop="@id/ok"
  27.             android:text="Cancel" />
  28. </RelativeLayout>


阅读(1339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~