Android布局管理器介绍

前言

一个人至少要拥有一个梦想,有一个理由坚强。心若没有栖息的地方,到哪里都是流浪。

线性布局

线性布局由LinearLayout类来代表,它会将容器里的组件一个挨着一个地排列起来。

代码示例1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="top|center_horizontal" >

    <Button
        android:id="@+id/bn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn1"
                />
     <Button
        android:id="@+id/bn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn2"
                />
      <Button
        android:id="@+id/bn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn3"
                />
</LinearLayout>

效果1

Screenshot_20171017-140522.png

代码示例2

<?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="vertical"
    android:gravity="left|center_vertical" >

    <Button
        android:id="@+id/bn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn1"
                />
     <Button
        android:id="@+id/bn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn2"
                />
      <Button
        android:id="@+id/bn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bn3"
                />
</LinearLayout>

效果2

Screenshot_20171017-141513.png

提示

android:orientation属性,控制各组件横向排列和竖向排列。
android:gravity属性,控制它所包含的子元素的对齐方式。
android:layout_gravity属性,设置该子元素在父容器中的对齐方式。

表格布局

表格布局由TableLayout所代表,TableLayout继承了LinearLayout,因此它的本质依然是线性布局管理器。除此之外,表格布局是通过添加TableRow、其他组件来控制表格的行数和列数。

代码示例

<?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="vertical" >

    <!-- 定义第一个表格布局,指定第二列允许收缩,第三列允许拉伸 -->
    <TableLayout android:id="@+id/tablelayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">

    <!-- 直接添加按钮,独自占一行 -->
    <Button
        android:id="@+id/bt1_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="独自占一行的按钮"
        />
    <TableRow>
       <Button
        android:id="@+id/bt1_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通按钮"
        />
        <Button
        android:id="@+id/bt1_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="收缩的按钮"
        />
         <Button
        android:id="@+id/bt1_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拉伸的按钮"
        />
    </TableRow>

    </TableLayout>

    <!-- 定义第二个表格布局,指定第二列隐藏 -->
    <TableLayout android:id="@+id/tablelayout02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">

    <!-- 直接添加按钮,独自占一行 -->
    <Button
        android:id="@+id/bt2_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="独自占一行的按钮"
        />

     <TableRow>
       <Button
        android:id="@+id/bt2_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通按钮"
        />
        <Button
        android:id="@+id/bt2_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐藏按钮"
        />
         <Button
        android:id="@+id/bt2_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通按钮"
        />
    </TableRow>


    </TableLayout>

    <!-- 定义第三个表格布局,指定第二列和第三列被拉伸 -->
    <TableLayout android:id="@+id/tablelayout03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2">
    <!-- 直接添加按钮,独自占一行 -->
    <Button
        android:id="@+id/bt3_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="独自占一行的按钮"
        />
        <TableRow>
           <Button
            android:id="@+id/bt3_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通按钮"
            />
            <Button
            android:id="@+id/bt3_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拉伸按钮"
            />
             <Button
            android:id="@+id/bt3_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拉伸按钮"
            />
         </TableRow>
    </TableLayout>
</LinearLayout>

效果

Screenshot_20171017-143543.png

提示

android:stretchColumns属性,设置允许被拉伸的列的列序号。
android:shringkColumns属性,设置允许被收缩的列的列序号。
android:collapseColumns属性,设置需要被隐藏的列的列序号。
上述属性如果有多个列序号,直接用逗号隔开。

帧布局

帧布局由FrameLayout所代表,FrameLayout直接继承了ViewGroup组件。

代码示例

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- 依次定义6个TextView,先定义的TextView位于底层 -->

    <TextView
        android:id="@+id/view01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="320pt"
        android:height="320pt"
        android:background="#f00"
        />

    <TextView
        android:id="@+id/view02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="280pt"
        android:height="280pt"
        android:background="#0f0"
        />

    <TextView
        android:id="@+id/view03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="240pt"
        android:height="240pt"
        android:background="#00f"
        />

    <TextView
        android:id="@+id/view04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="200pt"
        android:height="200pt"
        android:background="#ff0"
        />

    <TextView
        android:id="@+id/view05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="160pt"
        android:height="160pt"
        android:background="#f0f"
        />
    <TextView
        android:id="@+id/view06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="120pt"
        android:height="120pt"
        android:background="#0ff"
        />
</FrameLayout>

效果

Screenshot_20171017-150702.png

提示

帧布局的效果是把组件一个个地叠加在一起。

相对布局

相对布局由RelativeLayout所代表,相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的。

代码示例

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

    <!-- 定义该组件位于父容器直中间 -->
    <Button
        android:id="@+id/bt0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中心按钮"
        android:layout_centerInParent="true"
        />

    <!-- 定义该组件位于bt0组件的上方 -->
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上方按钮"
        android:layout_above="@id/bt0"
        android:layout_alignLeft="@id/bt0"
        />

    <!-- 定义该组件位于bt0组件的下方 -->
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上方按钮"
        android:layout_below="@id/bt0"
        android:layout_alignLeft="@id/bt0"
     />
    <!-- 定义该组件位于bt0组件的左侧 -->
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左侧按钮"
        android:layout_toLeftOf="@id/bt0"
        android:layout_alignTop="@id/bt0"
     />
    <!-- 定义该组件位于bt0组件的右侧 -->
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右侧按钮"
        android:layout_toRightOf="@id/bt0"
        android:layout_alignTop="@id/bt0"
     />
</RelativeLayout>

效果

Screenshot_20171017-152728.png

提示

layout_above属性,控制该子组件位于给出ID组件的上方。
layout_below属性,控制该子组件位于给出ID组件的下方。
layout_toLeftOf属性,控制该子组件位于给出ID组件的左侧。
layout_toRightOf属性,控制该子组件位于给出ID组件的右侧。
layout_alignTop属性,控制该子组件与给出ID组件的上边界对齐。
layout_alignBottom属性,控制该子组件与给出ID组件下上边界对齐。
layout_alignLeft属性,控制该子组件与给出ID组件的左边界对齐
layout_alignRight属性,控制该子组件与给出ID组件的右边界对齐。

网格布局

网格布局由GridLayout所代表,它的作用类似于HTML中的table标签,它把整个容器划分成rows x columns个网格,每个网格可以放置一个组件。除此之外,也可以设置一个组件横跨多少列、纵跨多少行。

代码示例

MainActivity.java
public class MainActivity extends Activity {

    GridLayout gridLayout;

    //定义16个按钮文本
    String[] chars = new String[]
            {
                "AC","+/-","%","÷",
                "7","8","9","x",
                "4","5","6","-",
                "1","2","3","+",
                "0","00",".","="
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);
        gridLayout = (GridLayout) findViewById(R.id.root);
        for(int i = 0; i < chars.length; i++)
        {
            Button bt = new Button(this);
            bt.setText(chars[i]);//设置按钮文本
            bt.setTextSize(40);//设置按钮的文本大小
            bt.setWidth(120);//设置按钮的宽度
            bt.setHeight(110);//设置按钮的高度
            //指定该按钮所在的行
            GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 1);
            //指定该按钮所在的列
            GridLayout.Spec columnSpec = GridLayout.spec(i % 4);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);

            //指定该按钮占满父容器
            params.setGravity(Gravity.FILL);
            gridLayout.addView(bt,params);

        }

    }


}
grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"
    android:id="@+id/root"
     >
    <!-- 定义一个横跨4列的文本框,并设置该文本框的前景色、背景色等属性 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:textSize="100sp"
        android:layout_marginLeft="2pt"
        android:layout_marginRight="2pt"
        android:padding="3pt"
        android:gravity="right"
        android:background="#eee"
        android:textColor="#000"
        android:text="0"
        />
</GridLayout>

效果

Screenshot_20171017-161435.png

提示

android:rowCount属性,设置该网格的行数量。
android:columnCount属性,设置该网格的列数量。
android:layout_rowSpan属性,设置该组件在GridLayout纵向上跨几行。
android:layout_columnSpan,设置该组件在GridLayout横向上跨几行。

绝对布局

绝对布局由AbsoluteLayout所代表。大部分时候,使用绝对布局不是一个好思路,绝对布局很难兼顾不同屏幕大小、分辨率的问题。因此AbsoluteLayout布局管理器已过时。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,513评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,312评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,124评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,529评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,937评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,913评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,084评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,816评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,593评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,788评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,267评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,601评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,265评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,158评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,953评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,066评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,852评论 2 277

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,594评论 25 707
  • 一.四大组件: Android四大组件分别为activity、service、content provider、b...
    Ludiwgbet阅读 1,667评论 0 39
  • 布局管理器可以管理安卓应用界面里的各种组件,根据运行平台管理组件的大小位置所有布局管理器都是ViewGrop的子类...
    CrazyBoomer阅读 3,169评论 0 2
  • 这个周末一起合租的室友们都去爬黄山了,因为囊中羞涩,我没有去。自己在家也有计划本上干不完的事情。 周五下班回家的时...
    mp好阅读 275评论 0 1
  • 我穿着洁白婚纱,踏着红地毯,红毯尽头,是一个俊郎少年向我邀手,脸上满是温柔笑意。 我说过无数次分手。你知道的,放声...
    甜觅觅阅读 624评论 0 7