(转载)Android界面布局浅议

来源:互联网 发布:合肥优化外包公司 编辑:程序博客网 时间:2024/05/20 07:37

一、线性布局LinearLayout与表格布局TableLayout的使用

二、相对布局实现较复杂的布局

1、LinearLayout的使用:

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/textView1"

    android:text="这是第一行"

    android:textColor="#0112aa"

    android:textSize="20pt"

    android:background="#00aa00"

    android:gravity="center_vertical"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:padding="23dip"

    android:layout_weight="1"

    android:singleLine="true"

    />

<TextView 

    android:id="@+id/textView2"

    android:text="这是第二行"

    android:textSize="12pt"

    android:background="#0000aa"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    />

</LinearLayout>

2、TableLayout的使用:

 

<?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"

    >

    <TableRow>

       <TextView

           android:text="@string/r1c1"

           android:background="#00aa00"

           android:padding="10dip" />

       <TextView

           android:text="@string/r1c1"

           android:background="#fd0011"

           android:gravity="right"

           android:padding="10dip"/>

       <TextView

           android:text="@string/r1c2"

           android:background="#0000aa"

           android:padding="10dip" />

    </TableRow>

    <TableRow>

       <TextView

           android:text="@string/r2c1"

           android:background="#fd0011"

           android:padding="10dip" />

      

       <TextView

           android:text="@string/r2c2"

           android:background="#00aa00"

           android:gravity="center_horizontal"

           android:padding="10dip" />

    </TableRow>

   

   

</TableLayout>

 

3、RalativeLayout的使用

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

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

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    >

    <TextView

       android:id="@+id/textView"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="type here" />

    <EditText

       android:id="@+id/editView"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:layout_below="@id/textView"

       android:text="在这里填写内容:" />  

    <Button

       android:id="@+id/button"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Cancel"

       android:layout_below="@id/editView"

       android:layout_alignParentRight="true"

       android:layout_marginLeft="12px" />

    <Button

       android:id="@+id/button2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_toLeftOf="@id/button"

       android:layout_below="@id/editView"

       android:text="Ok" />

</RelativeLayout>

RalativeLayout中各属性值的含义

分四类

第一类:

    android:layout_above: 将该控件置于指定控件之上

android:layout_below: 将该控件置于指定控件之下

android:layout_toLeftOf: 将该控件置于指定控件的左方

android:layout_toRightOf: 将该控件置于指定控件的右方

 

第二类(当前控件与其他控件):

android:layout_alignTop: 使该控件的上边缘和指定控件的上边缘对齐

android:layout_alignBottom: 使该控件的下边缘和指定控件的下边缘对齐

android:layout_alignLeft: 使该控件的左边缘和指定控件的左边缘对齐

android:layout_alignRight: 使该控件的右边缘和指定控件的右边缘对齐

android:layout_alignBaseline: 使该控件的baseline和指定控件的baseline对齐

 

第三类(当前控件与父控件):

android:layout_alignParentTop: 如果值为true,使该控件靠向父控件的上边缘

android:layout_alignParentBottom: 如果值为true,使该控件靠向父控件的下边缘

android:layout_alignParentLeft: 如果值为true,使该控件靠向父控件的左边缘

android:layout_alignParentRight: 如果值为true,使该控件靠向父控件的右边缘

 

第四类:

android:layout_centerHorizontal: 如果值为true,将该控件置于水平方向的中央

android:layout_centerVertical: 如果值为true,将该控件置于垂直方向的中央

android:layout_centerInParent: 如果值为true,将该控件置于水平方向和垂直方向的中央

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lganggang131/archive/2011/03/15/6251441.aspx

原创粉丝点击