android布局理解

1 线性布局LinearLayout 

   所有在其中的任意元素都会排成一条线  水平线或者垂直线

android:orientation="vertical|horizontal" 设置水平或者垂直布局

android:gravity表示对齐的方式  居中|左边|上面

android:background 设置的背景色

android:layout_width 表示宽度 除了设置数字的确定长度外 也可以相对父容器或者子容器的内容自动变化大小

        wrap_content 表示随着子内容的大小决定大小

       match_parent|fill_parent 表示填满父容器 

android:layout_height 高度和宽度设置类似

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#567555"
    android:gravity="top"                               
    android:orientation="vertical" >

<LinearLayout> 

2 表格布局TableLayout 

 以行和列的方式来进行布局  继承LinearLayout  拥有线性布局的所有属性

在布局中每个<TableRow>表示一行  其中每一个View控件表示一列  列的宽度由每一行中当前列最宽的那列宽度决定当前列的宽度

android:stretchColumns 表示自动拉伸的列编号 如果设置了  当前行没有完全填满时 设置的列自动拉伸填满表格行  可以指定 多个列用逗号隔开 或者 *所有列   

 android:collapseColumns 表示隐藏的列编号 设置了 该列不会显示  

android:shrinkColumns 表示自动收缩的列的编号 当行中某个列的长度超过了当前行的宽度 自动缩小到填充表格行  可以指定 多个列用逗号隔开 或者 *所有列   

举例:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#567555"
    android:gravity="center_vertical"
    android:stretchColumns="1"
    android:collapseColumns="0"
    
     >
    <TableRow android:id="@+id/tb1" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        <TextView android:text="qq号码"/>
        <EditText android:id="@+id/et1" android:layout_width="200dp" android:layout_height="wrap_content" android:inputType="text"/>
    </TableRow>
    <TableRow android:id="@+id/tb2"  android:layout_width="wrap_content" android:layout_height="wrap_content" >
        <TextView android:text="qq密码"/>
        <EditText android:id="@+id/et2" android:layout_width="200dp" android:layout_height="wrap_content"  android:inputType="text"/>
    </TableRow>

   <!--一条线-->
    <View android:layout_height="2dp" android:background="#867555"/>
</TableLayout>

3 相对布局 

就是将当前元素 置于指定元素的上下左右位置  常用的属性可以参考RelativeLayout类中的属性名称

toRightOf 当前元素的左边框 和另外一个元素的右边框 对齐 也就是放在指定元素的右边


可以在b的控件上指定    toRightOf="@id/a"

toLeftOf  将元素放在指定元素的左边

below 将元素放在指定元素的下边

above 将元素放在指定元素的上面

其他元素可以查看RelativeLayout属性

表示b元素的右边缘和a元素的左边缘对其  也就是放在a的右边


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >


    <ImageView
        android:id="@+id/iv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/iv"
        android:gravity="center_vertical"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/iv"
            android:text="189 9311 6326" >
        </TextView>


        <TextView
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/text"
            android:layout_toRightOf="@id/iv"
            android:text="甘肃兰州" />
    </LinearLayout>


    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#867555" 
        android:layout_below="@id/iv"
        />


</RelativeLayout>


效果如下:


4 FrameLayout

FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。

5 绝对布局

AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。



posted @ 2016-10-25 09:28  饺子吃遍天  阅读(203)  评论(0)    收藏  举报