对于android开发初学者而言,布局是真正入门的第一步,在android 开发中,布局的方式主要有五种:分别是LinearLayout ( 线性布局)、 TableLayout (表格布局) 、 RelativeLayout (相对布局)、FrameLayout( 帧布局 ) 、AbsoluteLayout(绝对布局) ,下面主要讲解LinearLayout布局,其中会重点的介绍LinearLayout布局中的一个参数:layout_weight,它可以在LinearLayout布局总调整各个组件占的面积权重。
下面看一下其中的几个参数,然后重点介绍layout_weight的功能。
android:layout_gravity ( 是本元素相对于父元素的重力方向 )
android:gravity (是本元素所有子元素的重力方向)
android:orientation (线性布局以列或行来显示内部子元素)
android:layout_weight (线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间。
若赋一个高于零的值,则将父视 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。 如果layout_weight值越小,则表示重要性越高,占的空间面积也会越大,反之,亦然。
下面新建一个简单的工程看一下这种布局的效果。
首先,我们在eclipse中新建一个Test001的android工程,接下来我们直接在mail.xml文件中去布局这种效果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--第一个嵌套的LinearLayout ,布局4个控件,注意,layout_height为 1 ,数值越小,表示权重越大,后面的第二个嵌套布局为 2,所以第一个嵌套布局占的面积越大,为2/(1+2)-->
<LinearLayout
android:layout_width="fill_parent"
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="#aa0000"
android:gravity="center_horizontal"
android:text="red" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00aa00"
android:gravity="center_horizontal"
android:text="green" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000aa"
android:gravity="center_horizontal"
android:text="blue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aaaa00"
android:gravity="center_horizontal"
android:text="yellow" />
</LinearLayout>
<!--第二个嵌套的LinearLayout , 垂直布局4个控件,注意,layout_height为2 ,值越小,重要性才越大,所以对比第一个嵌套的布局,占的面积为1/(1+2)-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row one"
android:textSize="15pt" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row two"
android:textSize="15pt" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row three"
android:textSize="15pt" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="row four"
android:textSize="15pt" />
</LinearLayout>
</LinearLayout>
布局好了直接运行,效果图如下:
参考:http://www.eoeandroid.com/thread-3278-1-1.html
整个android的布局和视图可以参考这篇博文点击打开链接。写的非常全!