Android-各种Layout对象之LinearLayout
刚接触Android,相比iPhone App,上手很容易,但是由于Google的开放性,市面上目前有各种各样分辨率的屏幕!!于是,布局变的很重要,不然你的Apk可能在一些机器上的用户体验很不好,This's a question!
写完自己的HelloWorld之后就开始找关于分辨率的资料,接着就是针对分辨率的解决方案,然后就是面对各种Layout对象了。Android的Layout对象不多,不过可以互相组合,所以。。。很多时候还是靠经验来组合,或者学习别人的布局思想。
(PS:iPhone由于硬件的垄断性,加上Interface Builder,界面的布局非常的简单,非常的兼容,非常的和谐。)
下边开始进入正题:
先是最常见到的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,那么,这两个文本框将等比例地放大(2个获得剩余空间的比例是1:1),并填满剩余的空间,没有设定weight的第三个文本框不会放大。
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值的容器对象。
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",来关闭这个设置。
下边来看一个例子:
< ?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </linearlayout><linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hi man" /> <edittext android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="4"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn2"/> </linearlayout>
(晕,xml解析有问题,第5行的第一个Linearlayout应该是在最后一行的!)
运行之后的样子:

这里有个比较奇怪的问题,LinearLayout之间的android:layout_weight是成反比显示的,但是LinearLayout内部的控件却是android:layout_weight是成正比显示的。
December 9th, 2009 - 11:41
[Reply]