Android 布局

 

Android用xml文件布局,创建一个布局:

 New -> XML -> Layout XMl File -> 输入xml文件名、布局类型 -> Finish

不管是对哪个文件、文件夹单击右键新建的布局文件,布局文件都会新建在res的layout目录下。

布局文件名只能使用小写字母a-z、数字0-9、下划线_

 

 

 

Android有5种常用布局:

1、LinearLayout   线性布局  

以水平方式或垂直方式显示控件。

常用属性:orientation="horizontal/vertical"     设置控件的排列方向

 

1 <LinearLayout
2         android:layout_width="match_parent"
3         android:layout_height="wrap_content"
4         android:orientation="horizontal">   
5 
6 </LinearLayout>

 

 

 

2、Relativelayout 相对布局  

以其他控件或父容器为参照,放置控件。控件常用的位置设置属性:

  • layout_centerInParent   父容器的水平、垂直中间
  • layout_centerHorizontal    父容器的水平中间
  • layout_centerVertical  父容器的垂直中间
  • layout_above/below    在某控件的上方/下方
  • layout_toLeft/RightOf    在某控件的左边、右边
  • layout_alignParentTop/Bottom/Left/Right   与父容器的某一边对齐
  • layout_alignTop/Bottom/Left/Right   与某控件的某一边对齐

如果未设置控件的相对位置,默认堆叠在左上角,后放置的控件优先级更高(可能会覆盖先放置的控件)。

 

 

 

3、AbsoluteLayout   绝对布局   

通过直接指定控件的坐标来确定控件的位置,但手机屏幕尺寸各不相同,这种布局不好用,不推荐使用,了解即可。

控件常用属性:

layout_x     横坐标

layout_y     纵坐标

 

 

 

4、FrameLayout   帧布局

 所有子控件都显示在左上角,堆叠放置,后放置的优先级更高,前面放置的控件往往会被覆盖。

帧布局适合图层设计,例如在图标上显示提示信息的数量。

FrameLayout的常用属性:

foreground     设置帧布局的前景图(会始终显示在所有子控件之上)

foregroundGravity    设置前景图的显示方式,可选的值:顶、右、底、左、拉伸(可指定方向)、裁剪(可指定方向)。

 

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@mipmap/xxx"
        android:foregroundGravity="fill">

</FrameLayout>

 

 

 

5、TableLayout    表格布局

 TableLayout需要和TableRow配合使用,TableRow表示表格的一行。

TableLayout的常用属性:

stretchColumns="index1,index2,..."   拉伸这些列(使用改行的的剩余长度)

shrinkColumns="index1,index2,..."    收缩这些列

collapseColumns="index1,index2,..."    隐藏这些列(不在占据空间)

index从0开始。

 

TableRow中的控件的常用属性:

layout_column="location"  指定该控件的显示位置。location从1开始。1表示此控件从第一个位置。

layout_span="num"  指定该控件占据几列,默认为1。

 

示例:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">
        
        <TableRow>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_span="1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_span="2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_span="2"/>
        </TableRow>

        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_span="1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_span="2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_span="2"/>
        </TableRow>
        
</TableLayout>

 

TableLayout不好控制。

 

 

 

 

控件的常用属性:

  • layout_marginXxx   设置某一边的margin
  • layout_paddingXxx    设置某一边的padding

 

posted @ 2019-05-14 22:20  chy_18883701161  阅读(275)  评论(0编辑  收藏  举报