android 应用界面布局

在使用布局前我们需要了解两个属性:

竖直方向布局:android:layout_height="wrap_content"

水平方向布局:android:layout_width="match_parent"

wrap_content表示包裹内容而不填充,match_parent表示铺满父容器,fill_parent和match _parent的意思一样 ,在2.2以上两个词都可以用,2.2以下的话,用fill_parent。

使用FrameLayout布局

FrameLayout对象就好比在一块屏幕上提前一定好的空白区域,然后可以填充元素在里面。所有元素被放置在区域的最左上,而且无法为这些元素制定确定的位置

<FrameLayout android:id="@+id/frameLayout1"

   android:layout_width="match_parent"

  android:layout_height="wrap_content">
    <Button android:layout_height="wrap_content" android:text="@string/ok" android:id="@+id/button1" android:layout_width="match_parent"></Button>
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</FrameLayout>

当放置了两个按钮,两个按钮会叠加在一起

image

 

 

使用LinearLayout布局

在android中常用的布局方式,它将自己包含的子元素按照一个方向(水平或垂直)进行排列

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"

      android:orientation="vertical">
  </LinearLayout>

在上面代码中加入两个按钮后界面如下

image

       方向可以通过 android:orientation="vertical"android:orientation="horizontal"来设置水平或是垂直排列

 

使用RelativeLayout布局

这个表示相对布局,它里面的元素按照相对位置来计算的。可以指定一个内部的元素A相对于元素B的位置

anroid中也有padding 、margin的概念。padding表示填充,margin表示边距。

布局设计到和父元素的对齐方式,和相对元素的对齐方式 ,间距等。

1.和父元素对齐属性:

android:layout_centerHrizontal         水平居中
android:layout_centerVertical           垂直居中
android:layout_centerInparent         相对于父元素完全居中
android:layout_alignParentBottom     贴紧父元素的下边缘
android:layout_alignParentLeft         贴紧父元素的左边缘
android:layout_alignParentRight        贴紧父元素的右边缘
android:layout_alignParentTop          贴紧父元素的上边缘
android:layout_alignWithParentIfMissing    如果对应的兄弟元素找不到的话就以父元素做参照物

2.和相对元素对齐属性:

android:layout_below          在某元素的下方
android:layout_above          在某元素的的上方
android:layout_toLeftOf       在某元素的右边

android:layout_alignTop       本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft      本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom  本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight     本元素的右边缘和某元素的的右边缘对齐

 

3.间距属性:

android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                  离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离

 

android中度量单位有:

px 像素,dip依赖于设备的像素,sp 带比例的像素 ,pt 点 ,in 英寸,mm 毫米

使用TableLayout布局

是一种表格布局,以行和列的形式排列。它里面包含了TableRow定义了每一行,每一行里可以添加需要的元素

<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
      <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
          <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"></TextView>
          <EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="30px"></EditText>
     </TableRow>
      <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
          <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"></TextView>
          <EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="30px"></EditText>
       </TableRow>
  </TableLayout>

posted @ 2011-05-31 11:14  张巍的博客  阅读(2689)  评论(0编辑  收藏  举报