【转】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>

相关文章
|
存储 安全 物联网
Android:Android 应用权限详解
这篇文章为大家系统的梳理一下 Android 权限相关的知识,在日常开发中,我们都用过权限,但是对于权限的一些细节我们可能掌握的还不够全面,这篇文章会全面的为大家介绍权限相关的知识。
1413 0
Android:Android 应用权限详解
|
Android开发 安全 数据格式
Android LocalBroadcastManager 的使用总结
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/53105494 本文出自【赵彦军的博客】 前言 在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。
1361 0
|
安全 编译器 API
Now in Android #15 —— 最新 Android 知识分享
Now in Android #15 —— 最新 Android 知识分享
Now in Android #15 —— 最新 Android 知识分享
|
传感器 存储 SQL
Android 值得学【Android 专题 2】
Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序。 四大组件 Android 系统四大组件分别是活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)和内容提供器(Content Provider)。其中活动是所有 Android 应用程序的门面,凡是在应用中你看得到的东西,都是放在活动中的。而服务就比较低调了,你无法看到它,但它会一直在后台默默地运行,即使用户退出了应用,服务仍然是可以继续运行的。广播接收器可以应用接收来自各处的广播消息,比如电话、短信等,当然你的应用同样也可以向外发出广播消息。内容提供器则为应用程序之间共享数
130 0
|
编解码 Java Android开发
Android小知识10则(上)(2018.8重编版)
Android小知识10则(下) 目录 前言 横竖屏锁定 不同分辨率的图标 将字符串写在资源文件中 为AlertDialog设置点击监听 ProgressDialog了解一下 最后 前言 Android的知识还是比较碎的, 日常积累很重要.
1070 0
|
XML Android开发 数据格式
|
Android开发 Ruby JavaScript