FrameLayout

FrameLayout is the simplest layout object. It is intended as a blank reserved space on your screen that you can later fill with a single object — for example, a picture that you'll swap out. All child elements are pinned to the top left corner of the screen; you cannot specify a location for a child of a FrameLayout. Later children will simply be drawn over earlier objects, partially or totally obscuring them (unless the newer object is transparent).

FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。




LinearLayout

A LinearLayout aligns all children in a single direction — vertically or horizontally, depending on what property you set on the LinearLayout. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). LinearLayout respects margins between children, and also gravity (right, center, or left alignment of a child).

LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。

LinearLayout also supports assigning a weight to individual children. This value allows children to expand to fill any remaining space on a screen. This prevents a list of small objects from being bunched to one end of a large screen, allowing them to expand to fill the space. Children specify a weight value, and any remaining space is assigned to children in the proportion of their declared weight. Default weight is zero. So, for example, if there are three text boxes, and two of them declare a weight of 1, two of them will expand equally to fill the remaining space, and the third will not grow any additional amount.

LinearLayout还支持为单独的子元素指定weight。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个weight值,剩余的空间就会按这些子元素指定的weight比例分配给这些子元素。默认的weight值为0。例如,如果有三个文本框,其中两个指定了weight值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。


Tip: To create a proportionate size layout on the screen, create a container object that is fill_parent, assign the children heights or widths of zero, and then assign relative weight values to each child, depending on what proportion of the screen each should take.

Tip:为了在屏幕上创建一个按比例安排大小的layout,需要根据这个屏幕上每个元素将按什么比例显示,创建一个指定fill_parent,子元素的height或width为0,且为每一个子元素分配weight值的容器对象。

The following two forms represent a LinearLayout with a set of elements: a button, some labels, some text boxes. Both have padding values to adjust the padding nicely. The text boxes have their width set to FILL_PARENT; other elements are set to WRAP_CONTENT. The gravity, by default, is left. The form on the left has weight values unset (0 by default); the form on the right has the comments text box weight set to 1. If the Name textbox had also been set to 1, the Name and Comments text boxes would be the same height.

下面的两个窗体采用LinearLayout,包含一组的元素:一个按钮,几个标签,几个文本框。两个窗体都为布局做了一番修饰。文本框的width被设置为FILL_PARENT;其它元素的width被设置为WRAP_CONTENT。默认的对齐方式为左对齐。左边的窗体没有设置 weight(默认为0);右边的窗体的comments文本框weight被设置为1。如果Name文本框也被设置为1,那么Name和 Comments这两个文本框将会有同样的高度。


Within a horizontal LinearLayout, items are aligned by the position of their text base line (the first line of the first list element — topmost or leftmost — is considered the reference line). This is so that people scanning elements in a form shouldn't have to jump up and down to read element text in neighboring elements. This can be turned off by setting android:baselineAligned="false" in the layout XML.

在一个水平排列的LinearLayout中,各项按他们的文本基线进行排列(第一列第一行的元素,即最上或最左,被设定为参考基线)。因此,人们在一个窗体中检索元素时,就不需要七上八下地读元素的文本了。我们可以在layout的XML中设置 android:baselineAligned="false",来关闭这个设置。




TableLayout

TableLayout positions its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells cannot span columns, as they can in HTML. The following image shows a table layout, with the invisible cell borders displayed as dotted lines.

TableLayout将子元素的位置分配到行或列中。一个TableLayout由许多的TableRow组成,每个 TableRow都会定义一个row(事实上,你可以定义其它的子对象,这在下面会解释到)。TableLayout容器不会显示row、cloumns 或cell的边框线。每个row拥有0个或多个的cell;每个cell拥有一个View对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML中的不一样。下图显示了一个TableLayout,图中的虚线代表不可视的单元格边框。


Columns can be hidden, can be marked to stretch to fill available screen space, or can be marked as shrinkable to force the column to shrink until the table fits the screen. See the reference documentation for this class for more details.

列可以被隐藏,也可以被设置为伸展的从而填充可利用的屏幕空间,也可以被设置为强制列收缩直到表格匹配屏幕大小。对于更详细信息,可以查看这个类的参考文档。




AbsoluteLayout

AbsoluteLayout enables children to specify exact x/y coordinates to display on the screen, where (0,0) is the upper left corner, and values increase as you move down or to the right. Margins are not supported, and overlapping elements are allowed (although not recommended). We generally recommend against using AbsoluteLayout unless you have good reasons to use it, because it is fairly rigid and does not work well with different device displays.

AbsoluteLayout可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用 AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。




RelativeLayout

RelativeLayout lets children specify their position relative to each other (specified by ID), or to the parent. So you can align two elements by right border, or make one below another, or centered in the screen. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. If using XML to specify this layout (as described later), a referenced element must be listed before you refer to it.

RelativeLayout允许子元素指定他们相对于其它元素或父元素的位置(通过ID指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用XML来指定这个layout,在你定义它之前,被关联的元素必须定义。

Here is an example relative layout with the visible and invisible elements outlined. The root screen layout object is a RelativeLayout object.

这是一个RelativeLayout例子,其中有可视的和不可视的元素。基础的屏幕layout对象是一个RelativeLayout对象。


This diagram shows the class names of the screen elements, followed by a list of the properties of each. Some of these properties are supported directly by the element, and some are supported by its LayoutParams member (subclass RelativeLayout for all the elements in this screen, because all elements are children of a RelativeLayout parent object). The RelativeLayout parameters are width, height, below, alignTop, toLeft, padding, and marginLeft. Note that some of these parameters support values relative to other children — hence the name RelativeLayout. These include the toLeft, alignTop, and below properties, which indicate the object to the left, top, and below respectively.

这个视图显示了屏幕元素的类名称,下面是每个元素的属性列表。这些属性一部份是由元素直接提供,另一部份是由容器的 LayoutParams成员(RelativeLayout的子类)提供。RelativeLayout参数有 width,height,below,alignTop,toLeft,padding和marginLeft。注意,这些参数中的一部份,其值是相对于其它子元素而言的,所以才RelativeLayout。这些参数包括toLeft,alignTop和below,用来指定相对于其它元素的左,上和下的位置。



重要View Group摘要

These objects all hold child UI elements. Some provide visible UI, and others only handle child layout.

这些对象拥有UI子元素。一些提供可视的UI,另一些只处理子元素的布局。

Class Description

AbsoluteLayout 可以通过精确的坐标(如屏幕像素)指定子对象相对父容器的位置
FrameLayout 负责显示单一对象的Layout
Gallery 一个以水平滚动方式显示有序图片列表的显示器
GridView 显示一个可滚动的有m列n行的表格
LinearLayout 以水平或垂直方式显示子元素的Layout。如果窗体的长度超过了屏幕的长度,将会出现滚动条
ListView 显示一个可滚动的单列列表
PopupList 一个独立的带边框的元素弹出列表
RelativeLayout 能够指定子对象相对于其它对象(如A在B的左边)或父对象(如在父容器的顶部)的位置
ScrollView 一个垂直的元素滚动列
Spinner 在一个单行文本框中,同时只显示一个有序列表中的一个项。类似于一个可以水平或垂直滚动的单行listbox
SurfaceView 提供直接访问一个可画图的界面。可以控制在界面顶部的子视图层。SurfaceView是提供给需要直接画像素而不是使用窗体部件的应用使用的。
TabHost 提供一个页签选择列表,监视点击并在一个页签被点击时保证应用切换屏幕。
TableLayout 一个拥有任意行和列的表格layout,每一个单元格拥有窗体部份。行会根据最大的列而自动调整大小。单元格边框不可见。
ViewFlipper 一个在单行文本框中同一时刻只显示一项的列表组件。它可以根据时间周期切换显示项,类似一个幻灯机。
ViewSwitcher 类似ViewFlipper

更多相关文章

  1. Android常用控件之GridView使用BaseAdapter
  2. [Android] 获取WebView的页面标题(Title)-----WebChromeClient.o
  3. Android(安卓)界面布局
  4. Android(安卓)WindowManager
  5. Android多媒体学习五:利用Service实现背景音乐的播放
  6. Android-AIDL通信
  7. Rokon引擎主要类介绍
  8. Android(java)学习笔记107:通过反射获得构造方法并且使用
  9. 基于Android的WebService开发例子

随机推荐

  1. 海康威视视频监控demo 源码+库文件
  2. Android(安卓)adb(Android(安卓)Debug Bri
  3. [置顶] Android(安卓)位于底部的Tab
  4. android操作sim卡联系人信息
  5. android 的相对布局的使用小记
  6. Android(安卓)中 OnTouch事件的研究
  7. android另一种访问包资源方式
  8. [入门三]Android应用开发入门五问
  9. android app的类响应式设计【半月谈投稿
  10. android 动画Animation属性大全(-)