android 开发布局之LinearLayout

对于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的布局和视图可以参考这篇博文点击打开链接。写的非常全!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值