您的位置:首页 > 移动开发 > Android开发

Android五大布局

2016-05-18 09:21 429 查看
一、线性布局LinearLayout

        包含的子控件将以横向或者竖向的方式排列

LinearLayout本身常用的两个属性

android:orientation = “vertical”

--------该属性决定子类控件的排布方式(vertical垂直      horizontal水平)

android:gravity =  “center”

--------该属性决定子类的xy位置(常用的属性值如下:)

1.center_gravity:垂直居中

2.center_horizontal:水平居中

3.center:水平垂直居中

4.right:子类控件位于当前布局的右边

5.left:子类控件位于当前布局的左边

6.bottom:子类控件位于当前布局的下面

子类控件在LinearLayout中常用的属性

android:layout_gravity = "bottom" -------指本身在当前父容器的XY的一个位置

android:layout_weight = "1"-------指本身控件占父容器的比例

如果将布局高度按比例分配,如果android:layout_height="match_parent"值为match_parent,则表示按反比分配

android:layout_height="wrap_content"值为wrap_content表示按比例分配

二、相对布局RelativeLayout

        包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列

        子类控件在RelativeLayout中常用的属性(相对父类容器的一个位置

        android:layout_alignParentLeft = "true"子类控件相对父类容器靠左边

        android:layout_alignParent  = "true"子类控件相对父类容器靠上边

        android:layout_marginLeft  = "40dp"子类控件距离父类容器靠左边的距离

        android:layout_marginTop  = "40dp"子类控件距离父类容器靠上边的距离

        android:layout_centerHorizontal = "true"子类控件相对父类容器水平居中

        android:layout_centerVertical = "true"子类控件相对父类容器垂直居中

       

        子类控件相对子类控件的一个位置

        android:layout_below = "@+id/button1"该控件位于给定id控件的底部

        android:layout_toRightOf = "@+id/button1"该控件位于给定id控件的右边

        android:layout_above = "@+id/button1"该控件位于给定id控件的上面

        android:layout_toLeftOf = "@+id/button1"该控件位于给定id控件的左边

        android:layout_alignBaseline= "@+id/button1"该控件的内容与给定id控件的内容在一条线上

        android:layout_alignBottom = "@+id/button1"该控件的底部边缘与给定id控件的底部边缘对齐

        android:layout_alignRight = "@+id/button1"该控件的右边边缘与给定id控件的右边边缘对齐

        android:layout_top = "@+id/button1"该控件的顶部边缘与给定id控件的顶部边缘对齐

 

三、帧布局FramLayout

帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
 
 简单的例子
①效果图:



 
② 核心代码:
main.xml
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView    
        android:layout_width="300dp"   
        android:layout_height="300dp"   
        android:background="#00BFFF"          
        /> 
    <TextView    
        android:layout_width="260dp"   
        android:layout_height="260dp"   
        android:background="#FFC0CB"          
        /> 
    <TextView    
        android:layout_width="220dp"   
        android:layout_height="220dp"   
        android:background="#0000FF"          
        /> 
</FrameLayout> 

 
四、表格布局

表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列

android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕

android:stretchColumns:尽量把指定的列填充空白部分

android:layout_column:控件放在指定的列

android:layout_span:该控件所跨越的列数
 
简单的列子:
①效果图:



 
② 核心代码:
 main.xml
 
<?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" 
    > 
    <TableRow> 
        <Button 
            android:text="Button1" 
            /> 
        <Button 
            android:text="Button2" 
            /> 
        <Button 
            android:text="Button3" 
            /> 
    </TableRow> 
    <TableRow> 
        <Button 
            android:text="Button4" 
            /> 
        <Button 
            android:layout_span="2" 
            android:text="Button5" 
            /> 
    </TableRow> 
      
</TableLayout>

五、 绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: