Activity 布局

来源:互联网 发布:ghost squad 知乎 编辑:程序博客网 时间:2024/06/05 06:50

1, LinearLayout 线性布局的使用方法

各控件呈线性排列

结构:

<LinearLayout>

       <TextView/>

       <Button/>

</LineraLayout>

 

示例:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView         android:id="@+id/firstText"        android:text="@string/LineOne"        android:gravity="center_vertical"        android:textSize="35pt"        android:background="#aa0000"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingLeft="10dip"        android:paddingRight="30dip"        android:paddingBottom="40dip"        android:layout_weight="1"        android:singleLine="true"/>        <TextView         android:id="@+id/secondText"        android:text="@string/LineTwo"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#0000aa"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        /></LinearLayout>

2,TableLayout 表格布局的使用方法

结构:

<TableLayout>

       <TableRow>

              <TextView/>

              <TextView/>

              <TextView/>

       </TableRow>

       <TableRow>

              <TextView/>

              <TextView/>

       </TableRow>

</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="2"><!--     <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" /> -->     <TableRow >        <TextView             android:text="@string/row1_column1"            android:background="#aa0000"            android:padding="3dip"/>                <TextView             android:text="@string/row1_column2"            android:background="#00aa00"            android:padding="3dip"/>                <TextView             android:text="@string/row1_column3"            android:background="#0000aa"            android:padding="3dip"/>            </TableRow>        <TableRow >        <TextView             android:text="@string/row2_column1"            android:padding="3dip"/>                <TextView             android:text="@string/row2_column2"            android:padding="3dip"/>            </TableRow>    </TableLayout>

3, LinearLayout 和 TableLayout 嵌套使用实现比较复杂的布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal"         android:layout_weight="1">               <TextView            android:text="@string/aaa"            android:background="#aa0000"            android:gravity="center_horizontal"                        andriod:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"/>                <TextView            andriod:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:background="#00aa00"            android:gravity="center_horizontal"            android:text="@string/bbb" />        <TextView            andriod:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:background="#0000aa"            android:gravity="center_horizontal"            android:text="@string/ccc" />        <TextView            andriod:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:background="#00aa00"            android:gravity="center_horizontal"            android:text="@string/ddd" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1"        android:orientation="vertical" >        <TableLayout            xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:stretchColumns="2" >            <TableRow>                <TextView                    android:background="#aa0000"                    android:padding="3dip"                    android:text="@string/row1_column1" />                <TextView                    android:background="#00aa00"                    android:padding="3dip"                    android:text="@string/row1_column2" />                <TextView                    android:background="#0000aa"                    android:padding="3dip"                    android:text="@string/row1_column3" />            </TableRow>            <TableRow>                <TextView                    android:padding="3dip"                    android:text="@string/row2_column1" />                <TextView                    android:padding="3dip"                    android:text="@string/row2_column2" />            </TableRow>        </TableLayout>    </LinearLayout></LinearLayout>

4,相对布局

(1) 相对布局的基本概念

(2) 相对布局与其他布局的区别

(3) 相对布局的常用属性介绍

andriod: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:layout_alignParentBottom  将该控件底部与父控件底部对齐

android:layout_alignParentLeft 将该控件的左边缘与父控件的左边缘对齐

android:layout_alignParentRight 将该控件的右边缘与父控件的右边缘对齐

android:layout_alignParentTop 将该控件的顶部与父控件的顶部对齐

 

 

android:layout_centerHorizontal 取值为true 或 false, 表示该控件将被置于水平方向中央

android:layout_centerInParent 是否将该控件置于父控件水平方向和垂直方向中央

android:layout_centerVertical是否将该控件置于垂直方向中央

示例(参考http://developer.android.com/resources/tutorials/views/hello-relativelayout.html):

<?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="fill_parent">    <TextView        android:id="@+id/label"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Type here:"/>    <EditText        android:id="@+id/entry"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:drawable/editbox_background"        android:layout_below="@id/label"/>    <Button        android:id="@+id/ok"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/entry"        android:layout_alignParentRight="true"        android:layout_marginLeft="10dip"        android:text="OK" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toLeftOf="@id/ok"        android:layout_alignTop="@id/ok"        android:text="Cancel" /></RelativeLayout>

呈现结果如下:


原创粉丝点击