7.2 LinearLayout布局详解

线性布局详解
本文介绍Android开发中常用的LinearLayout线性布局,包括垂直和水平布局的嵌套使用,并提供了一个具体的登录界面实例,展示了如何利用线性布局进行组件的排列。

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


图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开发案例驱动教程》第七章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值