LinearLayout线性布局,线性布局是所有布局中最常用的,它可以让其中的子元素垂直或水平的方式排列(通过排列方向的设置)。通常复杂的布局都是在LinearLayout布局中嵌套而成的。
下面看一个LinearLayout的例子,这个例子中有垂直和水平的嵌套使用,例子如下图7-12所示。
 

7.2 LinearLayout布局详解_开发


图7-12 LinearLayout
布局文件请参考代码清单7-14,完整代码请参考chapter7_2工程中linearlayout2.xml代码部分(chapter7_2/res/layout/linearlayout2.xml)。
【代码清单7-14】

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent"> 
  5. <TextView android:layout_width="fill_parent" 
  6. android:layout_height="wrap_content" android:text="@string/hello" 
  7. android:textSize="20dip" android:gravity="center" /> 
  8. <LinearLayout android:orientation="horizontal" 
  9. android:layout_width="fill_parent" android:layout_height="wrap_content"> 
  10. <TextView android:layout_width="wrap_content" 
  11. android:layout_height="wrap_content" android:text="@string/user" 
  12. android:textSize="15dip" /> 
  13. <EditText android:id="@+id/username" android:layout_width="fill_parent" 
  14. android:layout_height="wrap_content"></EditText> 
  15. </LinearLayout> 
  16. <LinearLayout android:orientation="horizontal" 
  17. android:layout_width="fill_parent" android:layout_height="wrap_content"> 
  18. <TextView android:layout_width="wrap_content" 
  19. android:layout_height="wrap_content" android:text="@string/pass" 
  20. android:textSize="15dip" /> 
  21. <EditText android:id="@+id/password" android:layout_width="fill_parent" 
  22. android:layout_height="wrap_content"></EditText> 
  23. </LinearLayout> 
  24. <LinearLayout android:orientation="horizontal" 
  25. android:layout_width="fill_parent" android:layout_height="wrap_content"> 
  26. <Button android:text="@string/loginbtn" android:id="@+id/Button01" 
  27. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
  28. <Button android:text="@string/registerbtn" android:id="@+id/Button02" 
  29. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
  30. </LinearLayout> 
  31. </LinearLayout>  


                                                                  出自《Android开发案例驱动教程》第七章