• 【转】LinearLayout:


    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>

  • 相关阅读:
    HDU_2191_多重背包
    HDU_1494_dp
    POJ_1088_dfs
    所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]
    畅通工程[HDU1863]
    还是畅通工程[HDU1233]
    最小生成树
    Who's in the Middle[HDU1157]
    Bungee Jumping[HDU1155]
    Is It A Tree?[HDU1325][PKU1308]
  • 原文地址:https://www.cnblogs.com/cappuccino/p/2112902.html
Copyright © 2020-2023  润新知