安卓开发06:布局-线性布局 LinearLayout

来源:互联网 发布:淘宝大牛韩代mlb正品吗 编辑:程序博客网 时间:2024/04/30 10:36

LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。线性布局是非常常用的一种布局方式。

请看一个布局例子:

[html] view plaincopyprint?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <!-- 大的框架,横着布局 -->  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <!-- 线性布局-竖着布局 -->  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="100dp"  
  17.             android:layout_height="fill_parent"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <Button  
  21.                 android:id="@+id/button1"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:text="Button1" />  
  25.   
  26.             <Button  
  27.                 android:id="@+id/button1"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:text="Button2" />  
  31.         </LinearLayout>  
  32.   
  33.         <!-- 线性布局-横着着布局 -->  
  34.         <LinearLayout  
  35.             android:layout_width="match_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:orientation="horizontal" >  
  38.   
  39.             <Button  
  40.                 android:id="@+id/button1"  
  41.                 android:layout_width="wrap_content"  
  42.                 android:layout_height="wrap_content"  
  43.                 android:text="Button3" />  
  44.   
  45.             <Button  
  46.                 android:id="@+id/button1"  
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="wrap_content"  
  49.                 android:text="Button4" />  
  50.         </LinearLayout>  
  51.     </LinearLayout>  
  52.   
  53. </LinearLayout>  

效果图:


线性布局框架的一个属性表:

属性描述layout_width指定View或ViewGroup的宽度layout_height指定View或ViewGroup的高度layout_marginTop指定View或ViewGroup上方的额外空间layout_marginBottom指定View或ViewGroup下方的额外空间layout_marginLeft指定View或ViewGroup左侧的额外空间layout_marginRight指定View或ViewGroup右侧的额外空间layout_gravity指定View或ViewGroup中的子视图的排列位置layout_weight指定指派给View或ViewGroup的额外空间尺寸layout_x指定View或ViewGroup的x坐标layout_y指定View或ViewGroup的y坐标


可以看到layout_width和layout_hight中经常有fill_parentwrap_contentmatch_parent来区分宽度和高度。这三者什么区别呢?

fill_parent

设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

wrap_content

设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

match_parent

Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了

视图和视图组概念:

视图:就是显示在屏幕上的一个组件(Widget)。View的例子:按钮(Button)、标签(TextView)和文本框(EditText)。每个“视图”(View)都继承自基类android.view.View。

视图组:可以包含一个或多个View。ViewGroup本身就是一种特殊的View,它提供了一个布局,可以使用这个布局去组织一系列的View视图。LinearLayout和FrameLayout。每个“ViewGroup”都继承自基类android.view.ViewGroup

原创粉丝点击