Google、在奋斗



Android开发中,我们可能会遇到过一些很复杂的布局,对于初学者来说,可能脑子会嗡的一下,“这么复杂!该怎么整?!”。

不要担心!再复杂的布局其实也是由简单地布局组成的,我们要学会将它分解成基本的布局,那么问题就迎刃而解了。

Android共有五种常见布局方式,分别是:LinearLayout(线性布局),FrameLayout(单帧布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。

下面首先看一下这些布局的关系

由此我们可以看出所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类。其它的一些布局都扩展自这5个类。

一、LinearLayout,线性布局方式

  这种布局比较常用,也比较简单,就是每个元素占一行,可声明为横向或纵向排放,也就是每个元素占一列。

  LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。

  LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之一,第二个文本框将占据剩余空间中的三分之二。

<LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="30dp"android:background="@android:color/holo_blue_bright" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="30dp"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="0.2"android:background="@android:color/holo_green_dark" /><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="0.3"android:background="@android:color/holo_orange_dark" /><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="0.3"android:background="@android:color/holo_blue_light" /><!–android:layout_weight="0.3" 作用:子控件占父控件的比例,即0.3/(0.2+0.3+0.3),值得注意的是如果有父布局中有一个子控件有weight值,那么其他同在父布局中的子控件都要有,否则就显示不出来。–></LinearLayout><LinearLayoutandroid:layout_width="80dp"android:layout_height="60dp"android:layout_marginBottom="20dp"android:layout_marginLeft="20dp"android:background="@android:color/holo_red_dark" ><!– 很容易看出android:layout_marginBottom="20dp"(离底部20dp)没有起作用,因为LinearLayout是一个控件挨着一个布局的 –></LinearLayout></LinearLayout>效果图:

二、Relative Layout,相对布局

  RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above,android:layout_centerVertical等。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

  RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。初学者可以利用可视化编辑器直接拖动控件到指定位置,来熟悉相关的位置属性。

<RelativeLayoutxmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/a1"android:layout_width="50dp"android:layout_height="60dp"android:background="@android:color/holo_green_dark"/> <TextViewandroid:id="@+id/a2"android:layout_width="70dp"android:layout_height="60dp"android:layout_alignParentTop="true"android:layout_marginLeft="36dp"android:layout_toRightOf="@+id/a1"android:background="@android:color/holo_blue_bright" /><TextViewandroid:id="@+id/a3"android:layout_width="30dp"android:layout_height="60dp"android:layout_toEndOf="@id/a1"android:background="@android:color/holo_orange_dark" /><!– layout_toEndOf和 layout_toRightOf有什么区别??–><TextViewandroid:id="@+id/a4"android:layout_width="70dp"android:layout_height="60dp"android:layout_marginLeft="36dp"android:layout_below="@id/a2"android:background="@android:color/black" /><TextViewandroid:id="@+id/a5"android:layout_width="90dp"android:layout_height="30dp"android:layout_marginLeft="36dp"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:background="@android:color/holo_purple" /></RelativeLayout>

接受失败,是我们不常听到或看到的一个命题,

Google、在奋斗

相关文章:

你感兴趣的文章:

标签云: