Android布局

 Android布局

            开发工具:
                                    Androidstudio 
                详解: 
             android开发中,用于界面布局的控件主要在Layouts分类中,常用的有线性布局控件LinearLayout表格布局控件TableLayout网格布局控件GridLayout框架布局控件 /桢布局控件FrameLayout等。本文将简要介绍下这几个布局控件,并通过一个实例演示这些控件的使用。
           
            1.线性布局控件LinearLayout
            

               1.1 属性--orientation 

                LinearLayout是布局中最基本的控件,也是最常用的布局控件。它有两种,一种是水平设置(horizontal);另一种是垂直设置vertical),通过属性orientation来设置,水平布局时只有一行可以有若干列;垂直布局时只有一列,可以有若干行。

                  1.2属性--gravity

                 android:gravity属性用来控制放置在本LinearLayout内的控件的对齐方式(控制所有子控件在父容器中的位置)

                android:layout_gravity属性是本LinearLayout控件相对于父容器的对齐方式

                 1.3属性--weight


             android:layout_weight它是一个比较重要的属性,它的意思就是“权重”。weight可以分配在父容器中的空间比例

               1.4 LinearLayout可以嵌套,也可以放置其他布局控件。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>


<LinearLayout

android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="左上按钮"

/>

</LinearLayout>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"

>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="左下按钮"

/>

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="中间按钮"

/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"


>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上按钮"
android:layout_gravity="top|right"


/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="右下按钮"
android:layout_gravity="bottom|right"

/>

</LinearLayout>

</LinearLayout>


</LinearLayout>


    案列如下:

                                Android布局


          2.表格布局控件TableLayout

          一个TableLayout里面有表格行控件TableRow,每个TableRow里布置各自的控件,好比是一个大架子里面放了很多个抽屉,各个抽屉里放着各自的东西,类型可以不一样,大小可以不一样如果不用TableRow控件的话就会默认占一行。表格布局有一个局限性就是它只跨列不跨行,但是可以对列进行伸(stretchColumns)缩(shrinkColumns)。它还可以对控件进行隐藏(collapse.....)--------表格布局一般来说比其它布局少用

           3.网格布局控件GridLayout

      GridLayout布局不仅可以跨列(columsCount)还可以跨行(rowCount),它就没有了TableLayout的局限--只能跨列。

        4.框架布局控件 /桢布局控件FrameLayout

        它是所有布局中最容易理解的,一个比较简单的布局控件。里面的控件关系为:后面控件会覆盖前面的控件。


         Android布局


          Android布局

       

             Android布局


          5.下面为布局控件的基本概念


Android布局

                6.总结:
                   Android开发中页面布局的重要性是不言而喻的。在Android软件开发中常用到的整体布局控件有:线性布局控件LinearLayout、表格布局控件TableLayout、绝对布局控件RelativeLayout、框架布局控件FrameLayout等。他们各有各的适用情况。而且这些控件的属性含义是互通的。只要你有创意、你就能开发出美观的界面来。