`
king_tt
  • 浏览: 2108944 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【Android 开发教程】LinearLayout线性布局

 
阅读更多

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。想知道LinearLayout是如何工作的,首先考虑一下典型的mail.xml文件。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. </LinearLayout>
在main.xnl中,可以看到,根节点元素是<LinearLayout>,它包含了一个<TextView>元素。这个<LinearLayout>元素控制了它所包含的视图views的顺序。

每一个View和ViewGroup有一些共同的属性,一些例子:

属性 描述
layout_width 指定View或ViewGroup的宽度
layout_height 指定View或ViewGroup的高度
layout_marginTop 指定View或ViewGroup上方的额外空间
layout_marginBottom 指定View或ViewGroup下方的额外空间
layout_marginLeft 指定View或ViewGroup左侧的额外空间
layout_marginRight 指定View或ViewGroup右侧的额外空间
layout_gravity 指定View或ViewGroup中的子视图的排列位置
layout_weight 指定指派给View或ViewGroup的额外空间尺寸
layout_x 指定View或ViewGroup的x坐标
layout_y 指定View或ViewGroup的y坐标

举个例子,<TextView>元素使用了fill_parent常量,所以的宽度填充了整个它所在布局的宽度。它的高度被设置成wrap_content常量,这意味着,它的高度将和它所在的布局一样。如果不想<TextView>占据整个屏幕,可以把layoutt_width元素设置成wrap_content,就像这样:

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="@string/hello"/><spanstyle="font-family:Arial,Helvetica,sans-serif;">
  5. </span>

以上的代码,把TextView的宽度设置成和它所包含的文本的长度一样。

请看如下的布局:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="100dp"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <Button
  11. android:layout_width="160dp"
  12. android:layout_height="wrap_content"
  13. android:text="Button"/>
  14. </LinearLayout>
这里,就把TextView和Button的宽度设置成了一个固定的值。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics