线性布局
垂直布局
<?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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.1"
android:id="@+id/No.1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.2"
android:id="@+id/No.2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.3"
android:id="@+id/No.3"
/>
</LinearLayout>
水平布局
<?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="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.1"
android:id="@+id/No.1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.2"
android:id="@+id/No.2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.3"
android:id="@+id/No.3"
/>
</LinearLayout>
*注意第五行代码的不同
纵横结合
<?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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.1"
android:id="@+id/No.1"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.2"
android:id="@+id/No.2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.3"
android:id="@+id/No.3"
/>
</LinearLayout>
</LinearLayout>
表格布局
<?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="horizontal" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0,1,2,3,4" >
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.1"
android:layout_column="1"
android:id="@+id/No.1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.2"
android:layout_column="2"
android:id="@+id/No.2"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.3"
android:layout_column="2"
android:id="@+id/No.3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.4"
android:layout_column="3"
android:id="@+id/No.4"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.5"
android:layout_column="3"
android:id="@+id/No.5"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.6"
android:layout_column="4"
android:id="@+id/No.6"
/>
</TableRow>
</TableLayout>
</LinearLayout>
相对布局
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/No.1"
android:id="@+id/No.1"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/No.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@id/No.1"
android:text="@string/No.2" />
</RelativeLayout>
</LinearLayout>
实例:微博登陆界面
<?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:background="#ffffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:text="@string/No.1"
android:layout_width="200sp"
android:layout_height="100sp"
android:textSize="40sp"
android:textColor="#FF7F00"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:gravity="center"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F00"
android:text="@string/No.2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20sp"
android:id="@+id/text1"
android:textSize="30sp"
/>
<EditText
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F00"
android:text="@string/No.3"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20sp"
android:id="@+id/text1"
android:textSize="30sp"
/>
<EditText
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CAE1FF"
android:text="@string/No.5"
android:id="@+id/No.5"
android:layout_alignParentRight="true"
android:textColor="#FF7F00"
android:textSize="30sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CAE1FF"
android:text="@string/No.4"
android:id="@+id/No.1"
android:layout_toLeftOf="@id/No.5"
android:textColor="#FF7F00"
android:textSize="30sp"
android:layout_marginRight="10dp"
/>
</RelativeLayout>
</LinearLayout>