老的Api(2.2或者2.3的)有五种布局:
LinearLayout线性布局,Relative Layout相对布局,AbsoluteLayout绝对位置布局,FrameLayout帧布局,TableLayout表格布局
而在最新的Api中只有两种布局:LinearLayout线性布局,Relative Layout相对布局
下面分析一下LinearLayout线性布局:
所有的孩子只有一个方向vertically或者horizontally(用android:orientation="vertical"设置),如果想产生换行的效果就要用布局嵌套,由于Android要解析XML,为了提高性能,一般不要包裹三层。
如下,用线性布局实现简单登录界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
android:layout_width="match_parent"
这里要用内容包裹,如果用match_parent就会把 下一行的内容遮盖住
android:layout_height="wrap_content"
android:orientation="horizontal"
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>