【转】LinearLayout:

简介: LinearLayout: LinearLayout是一个盒子模型(Box Model),以垂直或水平的方向,按照相对位置来排列所有的widgets或者其他的containers。所有被包含的widgets或者是containers都被堆放在container之后,因此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加上边框高度)。
LinearLayout:

LinearLayout是一个盒子模型(Box Model),以垂直或水平的方向,按照相对位置来排列所有的widgets或者其他的containers。所有被包含的widgets或者是containers都被堆放在container之后,因此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加上边框高度)。LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或者左对齐)。

LinearLayout还支持为其包含的widget或者是container指定填充权值。好处就是允许其包含的widget或者是container可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串widgets或者是containers挤成一堆的情况,而是允许他们放大填充空白。剩余的空间会按这些widgets或者是containers指定的权值比例分配屏幕。默认的 weight 值为0,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将Container剩余可用空间分割,分割大小具体取决于每一个widget或者是container的layout_weight及该权值在所有widgets或者是containers中的比例。例如,如果有三个文本框,其中两个指定的权值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大,按实际大小来显示。如果前两个文本框的取值一个为2,一个为1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。也就是权值越大,重要度越大。

如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越小。如果有LinearLayout A包含LinearLayout C,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。在LinearLayout嵌套的情况下,子LinearLayout必须要设置权值,否则默认的情况是未设置权值的子LinearLayout占据整个屏幕。

我们看一下效果图:
其中main.xml代码如下:

<?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

android:id ="@+id/lineLayout1"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2">

<!-如果未设置权值,则lineLayout1占据整个屏幕显示->

<TextView

android:text="block with weight 2 is smaller than the block with weight 1"

android:gravity="center_horizontal"

android:textSize="8pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

<LinearLayout

android:id ="@+id/lineLayout1"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:id = "@+id/red"

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:id = "@+id/white"

android:text="white"

android:gravity="center_horizontal"

android:background="#000000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

/>

<TextView

android:id = "@+id/green"

android:text="green"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

相关文章
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
50 0
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
62 0
|
XML Android开发 数据格式
|
XML Android开发 数据格式
Android零基础入门第25节:最简单最常用的LinearLayout线性布局
原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认为RelativeLayout相对布局,而在前面的示例学习中,我们只是简单利用了一下LinearLayout线性布局,那么接下来分别对其进行详细学习。
1257 0
|
XML Android开发 数据格式
Android零基础入门第28节:轻松掌握RelativeLayout相对布局
原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。
1223 0
|
Android开发 容器 数据格式