Android 学习笔记-布局

前言
布局就是把界面中的控件按照某种规律摆放在指定位置,主要是为了解决应用程序在不同手机中的显示问题。
在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来了解下Android开发中最常用的五大布局介绍和相关属性的设置。
概述
这里写图片描述

Android中常用的5大布局方式有以下几种:
线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
帧布局(FrameLayout):组件从屏幕左上方布局组件。
表格布局(TableLayout):按照行列方式布局组件。
相对布局(RelativeLayout):相对其它组件的布局方式。
绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。

布局的一些基本属性:
这里写图片描述

一:线性布局LinearLayout
线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。
常用的属性:
android:orientation:设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:权重(控制各个组件在布局中的相对大小比例)
简单线性布局中布局嵌套的应用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jc.myapp.MainActivity"
    android:weightSum="1">

    <!--match_parent:跟随屏幕大小-->
    <!--wrap_content:跟随内容大小-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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:text="右上按钮"
            />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中间按钮"
            android:layout_gravity="center"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        >

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                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:text="右下按钮"
            />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

效果图
这里写图片描述

二:表格布局TableLayout
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数

简单表格布局应用:

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


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="这是第一个lay的第一行"
                android:background="#ff0000"
                android:textColor="#aba6a6"
                android:layout_weight="1"
                android:layout_span="2"
                />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2行1列"
                android:background="#ff0000"
                android:textColor="#aba6a6"
                android:layout_weight="1"
                />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2行2列"
                android:background="#ffffff"
                android:textColor="#aba6a6"
                android:layout_weight="2"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2行3列"
                android:background="#ff00fb"
                android:textColor="#aba6a6"
                android:layout_weight="3"
                />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1"
        >

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二个layout第1行第1列"
                android:background="#00ff15"
                android:textColor="#aba6a6"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="c"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二个layout第1行第3列"
                android:background="#dd00ff"
                android:textColor="#aba6a6"
                />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第2行1列"
                android:background="#00ff15"
                android:textColor="#aba6a6"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="c"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第2行3列"
                android:background="#dd00ff"
                android:textColor="#aba6a6"
                />
        </TableRow>

    </TableLayout>

</LinearLayout>

效果图:
这里写图片描述

三:网格布局GridLayout
作为android 4.0 后新增的一个布局,与前面介绍过的TableLayout(表格布局)其实有点大同小异;
不过新增了一些东西
①跟LinearLayout(线性布局)一样,他可以设置容器中组件的对齐方式
②容器中的组件可以跨多行也可以跨多列(相比TableLayout直接放组件,占一行相比较)
因为是android 4.0新增的,API Level 14,在这个版本以前的sdk都需要导入项目

常用属性:
android:rowCount=”5” 设置网格布局中几行
android:columnCount=”4” 设置网格布局中几列
android:layout_columnSpan=”2” 跨列
android:layout_gravity=”fill_horizontal” 垂直填充
android:layout_columnSpan=”3” 跨行
android:layout_gravity=”fill_horizontal” 水平填充
scaleType=”fitXY” 设置在x轴和y轴填满

网格布局简单应用:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:layout_columnSpan="3"
        android:layout_gravity="fill_horizontal"
        />
    <Space />





</GridLayout>

效果图:
这里写图片描述

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

帧布局简单应用:

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/a3"
        android:scaleType="fitXY"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/a2"
        android:layout_gravity="center"
        />

</FrameLayout>

效果图:
这里写图片描述

总结:
本文对Android五大布局介绍&属性设置进行了基础简单的介绍。接下来我会介绍继续介绍Android开发中的相关知识。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值