布局Layout管理

布局Layout管理

  布局即是指Activity中组件的呈现方式,即组件大小、间距和对齐方式等。

  Android提供了两种创建布局的方式:

    1.在XML配置文件中声明(推荐)。

    2.在程序中通过代码直接实例化布局及其组件。

 

在Android中常见的布局方式:

  线性布局(LinearLayout):按照垂直或者水平方向布局组件。

  帧布局(FrameLayout):组件从屏幕的左上角坐标布局组件。

  表格布局(TableLayout):按照行列方式布局组件。

  相对布局(RelativeLayout):相对其他组件的布局方式。

  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。(已废)。

  各种布局方式之间可以互相嵌套,只需要将布局理解成为一个容器控件即可,官方的说法叫View Group,见http://developer.android.com/guide/topics/ui/overview.html

 

线性布局LinearLayout

  线性布局是将子组件按照垂直或者水平方向来布局。  

 1:作用

   使用线性布局之后,存在于该布局之内的控件将一个挨着一个的排列起来。排列方式可以水平,也可以垂直,具体的设置可以由下列属性指定:

   2:属性

   (1) android:layout_width和android:layout_height

   这两个属性为必须有的属性,分别标志元素的宽度与高度。有三个值

   ◆match_parent:充满父级元素,后来版本采用的方式,对于顶级元素而言,父级元素为整个手机屏幕

   ◆fill_parent:充满父级元素,较早使用的一种方式

wrap_content:自适应,只显示足够的空间

   (2)android:orientation

   指明存在于其中的元素的排列方式。有两个取值

   ◆vertical:存在于其中的元素,单独占据一行,垂直排列

   ◆horizontal:存在于其中的元素,单独占据一列,水平排列

   (3)android:gravity

   用于设置View内部元素的排列方式,取值可以包括:left(左对齐),right(右对齐),top(上对齐),bottom(下对齐),center(居中对齐),center_horizontal(水平居中),center_vertical(垂直居中)等,还可有组合使用,如right|top(右上)。

   比如:一个按钮可以设置其上文字在按钮上的位置、一个布局可以设置存在于其中控件的位置等

   (4)android:layout_gravity

   用于设置该view在其父view中的对齐方式,取值包括:left(左对齐),right(右对齐),top(上对齐),bottom(下对齐),center(居中对齐),center_horizontal(水平居中),center_vertical(垂直居中)等,还可有组合使用,如right|top(右上)。

   比如:一个按钮可以设置自己在其父级View上的位置

   (5)android:layout_weight

   权重,根据设定值实现通过百分比来进行布局的目的。取值建议设定为整数值。此属性仅在LinearLayout布局下存在。

   当android:layout_width和android:layout_height设定为match_parent时,对比中的判断为反相关,取值越大,比重越小

android:layout_width和android:layout_height设定为wrap_content时,对比中的判断为正相关,取值越小,比重越大

  一般情况下都是在LinearLayout的开头就设定方向和宽高,至于里面摆放的控件,就具体设定其控件的属性。

  帮助文档:

  http://developer.android.com/reference/android/widget/LinearLayout.html

  例子:

  自己新建一个布局文件,放在res\layout文件夹里(貌似文件名必须都是小写字母。

  布局文件如下:

LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     
     <TextView
         android:id = "@+id/firstText"
         android:text="@string/TextOne"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         >
     </TextView>
          
     <TextView
         android:id = "@+id/secondText"
         android:text="@string/TextTwo"
         android:gravity="center_vertical"
         android:background="@color/red"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         >
     </TextView>
               
     <TextView
         android:id = "@+id/thirdText"
         android:text="@string/TextThree"
         android:gravity="center"
         android:textColor="@color/blue"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="3"
         >
     </TextView>
    
    
 </LinearLayout>


      运行结果如下:

                        

 

帧布局FrameLayout

  http://developer.android.com/reference/android/widget/FrameLayout.html

  帧布局是从屏幕的左上角坐标(0,0)开始布局,多个组件层叠排序,后面的组件覆盖前面的组件。

  帧布局中的每个组件都是一个画面,且存在其中的控件以罗列的方式显示,只有最上面的那个控件消失之后,后面的页面才会依次显示。

  把上文中的LinearLayout改为FrameLayout,

  程序运行结果如下:

  

  其中红色是第二个TextView的背景色,而第三个TextView没有背景色,直接覆盖在上面了。

 

表格布局TableLayout

  http://developer.android.com/reference/android/widget/TableLayout.html

  表格布局以行、列表格的方式布局子组件。

  TableLayout中使用TableRow来定义多行。

  TableLayout中如果不用TableRow,则所有控件从上到下排列。

  用了TableRow,每个TableRow中的控件构成多列。

  如下图,每一个TableRow中放两个TextView:

  

 

  属性android:stretchColumns表示拉伸列,表示如果填充不满时,拉伸该序号(序号从0开始)的列,填满空间。

  比如设置

  android:stretchColumns="0"后,拉伸第一列。

  如下图:

  

  android:collapseColumns表示隐藏指定的列。

  表格布局的代码:

TableLayout

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0"
    
    >
     <TableRow>
     <TextView
         android:id = "@+id/firstText"
         android:text="@string/TextOne"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"

         >
     </TextView>
          
     <TextView
         android:id = "@+id/secondText"
         android:text="@string/TextTwo"
         android:gravity="center_vertical"
         android:background="@color/red"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"

         >
     </TextView>
     </TableRow>
      
     <TableRow>
     <TextView
         android:id = "@+id/thirdText"
         android:text="@string/TextThree"
         android:gravity="center_vertical"
         android:textColor="@color/blue"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"

         >
     </TextView>
          
     <TextView
         android:id = "@+id/fourthText"
         android:text="@string/TextFour"
         android:gravity="center_vertical"
         android:background="@color/green"
         android:textColor="@color/blue"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"

         >
     </TextView>
     </TableRow>

    
    
 </TableLayout>

相对布局RelativeLayout

  相对布局按照组件之间的相对位置来布局,如在某个组件的左右上下等。

  http://developer.android.com/reference/android/widget/RelativeLayout.html

  通过设置控件的属性来设置控件的相对位置。

  属性可以分为四大类:

  第一类:上下左右四个相对位置。

  第二类:边缘对齐的五个属性(加上一个基线对齐)。

  第三类:是否和父控件在上下左右边缘对齐的四个属性。

  第四类:居中方式的三个属性。

  前两类设置时设置指定控件的id,后两类的值为true或者false。

   (1)相对于父级控件位置的属性

   ◆ android:layout_alignParentLeft="true":存在父级控件的左侧

   ◆android:layout_alignParentTop="true":存在附近控件的顶部

   ◆ android:layout_alignParentBottom="true":存在父级控件的下部

   ◆ android:layout_alignParentRight="true":存在父级控件的右侧

  (2)相对于其他控件的位置属性

   ◆ android:layout_toLeftOf="true":存在于其他控件的左侧

   ◆ android:layout_toRightOf="true":存在于其他控件的右侧

   ◆android:layout_above="true":存在于其他控件的上面

   ◆android:layout_below="true":存在于其他控件的下面


 

绝对布局AbsoluteLayout

  绝对布局通过指定子组件的确切XY坐标位置,该类已经过期,可以使用其他布局代替之。

 

资源

  布局教程汇总:http://www.apkbus.com/android-50865-1-1.html

  官方文档教程:http://developer.android.com/guide/topics/ui/declaring-layout.html

 

转载 http://www.cnblogs.com/mengdd/archive/2012/12/19/2825382.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值