布局layout
布局分为:线性布局(linear_layout)、相对布局(relative_layout)、表格布局(table_layout).
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
登录页面,不同方式代码:
表格布局
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/system" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userpass" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:id="@+id/login_id"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:id="@+id/cancel_id"
/>
</TableRow>
</TableLayout>
相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/system"
android:id="@+id/t1_id"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:layout_below="@id/t1_id"
android:id="@+id/name_id"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="30px"
android:layout_toRightOf="@id/name_id"
android:layout_below="@id/t1_id"
android:id="@+id/e1_id"
></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userpass"
android:id="@+id/pass_id"
android:layout_below="@id/name_id"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="30px"
android:layout_below="@id/e1_id"
android:layout_toRightOf="@+id/pass_id"
android:id="@+id/e2_id"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:layout_below="@id/e2_id"
android:layout_alignParentRight="true"
android:id="@+id/cancel_id"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:layout_below="@id/e2_id"
android:layout_toLeftOf="@id/cancel_id"
/>
</RelativeLayout>
嵌套布局:
<?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="fill_parent"
android:layout_height="wrap_content"
android:text="@string/system"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:id="@+id/name_id"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/name_id"
android:id="@+id/e1_id"
>
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/ >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userpass"
android:id="@+id/pass_id"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/pass_id"
/>
</RelativeLayout>
<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/cancel"
android:layout_alignParentRight="true"
android:id="@+id/cancel_id"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:layout_toLeftOf="@id/cancel_id"
/>
</RelativeLayout>
</LinearLayout>