Android基础(二) LinearLayout

 
811人阅读 评论(0) 收藏 举报

 

一、概述  

线性布局(LinearLayout)是所有Android布局中最简单的一种,它将widget以行或列进行排列。配置LinearLayout有5个重要的属性分别是:填充模式(fill modal)、比重(weight)、排列方式(gravity)、padding。

 

二、填充模式

    填充模式(fill modal) 包括填充指定的像素(例如:125px)、wrap_content、fill_parent.


三、比重(weight)

    设置widgets瓜分剩余空间时的比重。以下例子分别演示两个按钮 1:1 和 1:2时的显示情况。

【效果图】
 1:1 
 
1:2
 

【代码要点】 
weight12.xml:两个widgets竖直排列,分割剩余空间的比重为1:2时的布局
代码.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7. <Button  
  8. android:id="@+id/widget31"  
  9. android:layout_width="fill_parent"  
  10. android:layout_height="wrap_content"  
  11. android:text="weight = 1"  
  12. android:layout_weight="1"  
  13. />  
  14. <Button  
  15. android:id="@+id/widget32"  
  16. android:layout_width="fill_parent"  
  17. android:layout_height="wrap_content"  
  18. android:text="weight = 2"  
  19. android:layout_weight="2"  
  20. />  
  21. </LinearLayout>  
 

三、排列方式(gravity)

    设置widgets的排列方式,默认为靠左上排列,也可以设置其他的排列方式:靠左、靠右、居中、靠下方等...
    当同一行有多个控件时,常用的排列方式为靠下(bottom) 和 竖直居中;

    当同一列有多个控件时,常用的排列方式为靠左、居中、靠右; 
    出来通过在.xml中定义排列方式外,还可以在代码中设置:setGravity().

 

【效果图】
在水平线性布局下对三个按钮进行靠下方排列。


在竖直线性布局下对三个按钮进行水平居中排列。
 

 

【代码要点】
center.xml:竖直线性布局下对三个按钮进行水平居中排列。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical">  
  7. <Button  
  8. android:id="@+id/widget31"  
  9. android:layout_width="wrap_content"  
  10. android:layout_height="wrap_content"  
  11. android:text="center-1"  
  12. android:layout_gravity="center_horizontal"  
  13. />  
  14. <Button  
  15. android:id="@+id/widget32"  
  16. android:layout_width="wrap_content"  
  17. android:layout_height="wrap_content"  
  18. android:text="center-2"  
  19. android:layout_gravity="center_horizontal"  
  20. />  
  21. <Button  
  22. android:id="@+id/widget33"  
  23. android:layout_width="wrap_content"  
  24. android:layout_height="wrap_content"  
  25. android:text="center-3"  
  26. android:layout_gravity="center_horizontal"  
  27. />  
  28. </LinearLayout>  
 

 

四、padding

    设置widget控件外沿与内容的边距.
      
 【效果图】
  一个按钮填充整个屏幕,设置文字内容与按钮外沿的左边距(paddingLeft)是"90px"
 

【代码要点】
padding.xml 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6. <Button  
  7. android:id="@+id/widget31"  
  8. android:layout_width="fill_parent"  
  9. android:layout_height="fill_parent"  
  10. android:text="padding 10px"  
  11. android:paddingLeft="90px"  
  12. />  
  13. </LinearLayout>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值