线性布局学习
1. 线性布局简介
Android线性布局是Android五种布局中最常见的一种布局(其他四种布局分别是框架布局、相对布局、绝对布局、表格布局)。线性布局可以认为是一个视图组,他的所有子视图都在水平或者垂直方向对齐。线性布局的所有指示图排列都是一个靠着一个。如下图所示:
线性布局可以分为水平布局和垂直布局两种。可以通过android:oriention属性或者在Java代码中通过代码中通过layout.setOrientation(LinearLayout.HORIZONTAL)进行设置。
2. 线性布局常用参数的使用:
Ø android:orientation=”vertical|horizontal”
表示线性布局的方向:垂直布局|水平布局
Ø android:layout_width=””fill_parent(match_parent)|wrap_content
在父控件中当前控件的宽,fill_parent表示填满父控件,而wrap_content表示刚好填充当前控件的内容
Ø android:layout_height=””fill_parent(match_parent)|wrap_content
在父控件中当前控件的高,fill_parent表示填满父控件,而wrap_content表示刚好填充当前控件的内容
Ø android:gravity=”top|bottom….”
用于控制布局中控件的对齐方式,对于没有子控件的控件设置此属性表示其内容的对齐方式。若存在子控件则表示子控件的对齐方式。Gravity可以设为多个属性,使用“|”连接。
android:layout_weight=”number”(注意没有android: layout_weight这个参数)
通过设置该参数可以控制各个控件在布局中的相对大小。layout_weight是一个非负整数线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。
注意:
android:gravity与android:layout_gravity的区别:android:gravity定义了这个元素内所有子元素对于这个元素的布局,比如一个TextView内部文字的对齐方式;android:layout_gravity定义了这个元素相对于父元素(比如Layout)的布局,比如一个TextView在整个Layout中的位置。
3. 线性布局实例
线性布局的xml配置文件如下所示:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aa0000" android:gravity="fill_vertical" android:text="red"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffffff" android:text="white" android:textColor="#ff0000"/> </LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal">
<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00aa00" android:text="green" android:textColor="#ff0000"/>
<TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#0000aa" android:text="blue"/> </LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#000000" android:text="black"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aaaa00" android:text="yellow"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#00aaaa" android:text="unkown"/> </LinearLayout> </LinearLayout> </LinearLayout> |
运行结果如下所示:
参考资料如下:
1. http://wiki.eoe.cn/page/Linear_Layout.html
2. http://hi.baidu.com/xxj050/item/24d799b88f5173a0eaba938c
3. http://hi.baidu.com/hoyah/item/79f8390859c69dda72e676f1