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 
Android基础(二) LinearLayout_第1张图片 
1:2
Android基础(二) LinearLayout_第2张图片 

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

view plain copy to clipboard print ?
  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>  
<?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"> <Button android:id="@+id/widget31" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="weight = 1" android:layout_weight="1" /> <Button android:id="@+id/widget32" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="weight = 2" android:layout_weight="2" /> </LinearLayout>  

三、排列方式(gravity)

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

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

 

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

Android基础(二) LinearLayout_第3张图片
在竖直线性布局下对三个按钮进行水平居中排列。
 Android基础(二) LinearLayout_第4张图片

 

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

view plain copy to clipboard print ?
  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>  
<?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"> <Button android:id="@+id/widget31" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="center-1" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/widget32" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="center-2" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/widget33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="center-3" android:layout_gravity="center_horizontal" /> </LinearLayout> 

 

四、padding

    设置widget控件外沿与内容的边距.
    Android基础(二) LinearLayout_第5张图片  
 【效果图】
  一个按钮填充整个屏幕,设置文字内容与按钮外沿的左边距(paddingLeft)是"90px"
Android基础(二) LinearLayout_第6张图片 

【代码要点】
padding.xml 

view plain copy to clipboard print ?
  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>  

你可能感兴趣的:(Android基础(二) LinearLayout)