Android studio 六大基本布局详解_android studio布局-程序员宅基地

技术标签: android  六大布局  

Android中常用的布局方式有以下几种:

  • 线性布局LinearLayout

  • 相对布局RelativeLayout

  • 表格布局TableLayout

  • 层布局FrameLayout

  • 绝对布局AbsoluteLayout

  • 网格布局GridLayout

用的相对较多的是线性布局和相对布局。接下来重点演示这两种布局
其中,表格布局是线性布局的子类。网格布局是android 4.0后新增的布局。

(一)线性布局LinearLayout

线性布局中最重要的属性:orientation
horizontal(水平布局)和vertical(垂直布局)两种方式

属性名

  • orientation 布局方式,有horizontal(水平布局)和vertical(垂直布局)两种方式

  • id 组件名称

  • layout_width 该组件的宽度

  • layout_height 该组件的高度

  • layout_weight 权重

  • layout_gravity 该组件(在父容器)中的对齐方式

  • gravity 该组件所含子组件在其内部的对齐方式

  • background 设置背景图片或填充颜色

效果图

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/gray"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:text="权重1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="权重2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="权重3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="权重4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="权重5"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="20dp"
        android:background="@color/teal_200"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第一个布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:background="@color/purple"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第二个布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:background="@color/teal"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第三个布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</LinearLayout>

(二)相对布局RelativeLayout

属性:

  • android:layout_marginTop=“25dip” //顶部距离
  • android:gravity=“left” //空间布局位置
  • android:layout_marginLeft="15dip //距离左边距

相对于给定ID控件

  • android:layout_above 将该控件的底部置于给定ID的控件之上;

  • android:layout_below 将该控件的底部置于给定ID的控件之下;

  • android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐;

  • android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐;

  • android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;

  • android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐;

  • android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐;

  • android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐;

  • android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;

相对于父组件

  • android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐;
  • android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
  • android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐;
  • android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐;

居中

  • android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
  • android:layout_centerVertical 如果为true,将该控件的置于垂直居中;
  • android:layout_centerInParent 如果为true,将该控件的置于父控件的中央;

指定移动像素

  • android:layout_marginTop 上偏移的值;
  • android:layout_marginBottom 下偏移的值;
  • android:layout_marginLeft   左偏移的值;
  • android:layout_marginRight   右偏移的值;

效果图

在这里插入图片描述

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

    <TextView
        android:gravity="center"
        android:background="@color/teal"
        android:text="text1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:id="@+id/tv_two"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_centerInParent="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_above="@+id/tv_two"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text4"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
</RelativeLayout>

(三)表格布局TableLayout

属性

三个常用属性

  • android:collapseColumns:设置需要被隐藏的列的序号
  • android:shrinkColumns:设置允许被收缩的列的列序号
  • android:stretchColumns:设置运行被拉伸的列的列序号

(四)帧布局FrameLayout

FrameLayout(帧布局)可以说是六大布局中最为简单的一个布局,这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多;帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看到最上面的那个组件!后续添加的控件会覆盖前一个!虽然默认会将控件放置在左上角,但是我们也可以通过layout_gravity属性,指定到其他的位置!

效果图

在这里插入图片描述
xml布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/gray"
    android:layout_height="match_parent">
    <TextView
        android:background="#000000"
        android:layout_width="fill_parent"
        android:layout_height="180dp"/>
    <TextView
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="130dp"/>
    <TextView
        android:background="#ff00ff"
        android:layout_width="fill_parent"
        android:layout_height="100dp"/>
    <TextView
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_height="50dp"/>
</FrameLayout>

(五)绝对布局AbsoluteLayout

属性:

  • 绝对布局又可以叫做坐标布局,可以直接指定子元素的绝对位置(xy)
  • 由于手机屏幕尺寸差别比较大使用绝对定位的适应性会比较差,在屏幕的适配上有缺陷

常用属性:

  • android:foreground:*设置改帧布局容器的前景图像
  • android:foregroundGravity:设置前景图像显示的位置
  • android:layout_x=”” 控制当前子类控件的x位置
  • android:layout_y=”” 控制当前子类控件的y位置

效果图
在这里插入图片描述

.xml布局

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:background="@color/gray"
   android:layout_height="match_parent">
   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_x="26dp"
       android:layout_y="124dp"
       android:text="Button" />
   <Button
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_x="66dp"
       android:layout_y="224dp"
       android:text="Button" />

   <Button
       android:id="@+id/button3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_x="126dp"
       android:layout_y="323dp"
       android:text="Button" />
</AbsoluteLayout>

(六)网格布局GridLayout

和之前的TableLayout(表格布局) 有点类似,不过网格布局的好处是:

  • 可以自己设置布局中组件的排列方式
  • 可以自定义网格布局有多少行,多少列
  • 可以直接设置组件位于某行某列
  • 可以设置组件横跨几行或者几列

效果图

在这里插入图片描述

.xml布局:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:orientation="horizontal"
    android:rowCount="6" >
    <TextView
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#15CBE3"
        android:text="0"
        android:textSize="50sp" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="回退" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="清空" />

    <Button android:text="1" />

    <Button android:text="2" />

    <Button android:text="3" />
    <Button android:text="+" />


    <Button android:text="4" />

    <Button android:text="5" />

    <Button android:text="6" />
    <Button android:text="-" />


    <Button android:text="7" />

    <Button android:text="8" />

    <Button android:text="9" />
    <Button android:text="*" />


    <Button android:text="0" />
    <Button android:text="." />

    <Button android:text="=" />
    <Button android:text="/" />
</GridLayout>

<GridLayout android:layout_width=“fill_parent”:网格布局宽度为填满屏幕

<GridLayout android:layout_height=“wrap_content”:网格布局高度为包裹内容

<GridLayout android:columnCount=“4”:网格布局设置 4 列

<GridLayout android:rowCount=“6”:网格布局设置 6 行

<GridLayout android:layout_columnSpan=“2”:清空和回退横跨两列

<GridLayout android:orientation=“horizontal”:网格布局设置为水平布局

以上是六大布局基本讲解

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/afufufufu/article/details/116530968

智能推荐

注解--注释注解_注解@__-程序员宅基地

文章浏览阅读669次。黑马程序员注解–注释注解测试类pro注解解析测试主方法package A_Part02.annotation02;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType_注解@__

卡通人物手脚的绘画方法和技巧-程序员宅基地

文章浏览阅读150次。俗话说“画人难画手”,这说明画手是比较难的,为此我们应先了解手的结构。  手的结构可分为手掌和手腕两部分,要将手掌看成一个不规则的五边形,作画时,先要将这两部分看作一个整体,画出手的边线,再定出大拇指的位置。要明确每个手指的长度是各不相同的,手指的关节部位要适当弯曲,在特写画面中,要画出手指的两个关节,特别要强调一下拇指和小指的外轮廓线,这样会更有立体感。   [img]/uplo..._opengl绘制卡通形象

网页右边没有滚动条,内容超出也看不到怎么办?_网页没有下拉滚动条怎么办-程序员宅基地

文章浏览阅读4.4k次。遇到的问题:博主正在进行vue组件开发,将主页面添加了组件之后,发现在浏览器中打开内容超出但没有滚动条解决方法:我们可以找一下css中的设置,去掉 html和body中的overflow:hidden;即可!..._网页没有下拉滚动条怎么办

Linux中源代码安装及ps命令_linux安装ps命令-程序员宅基地

文章浏览阅读2.5k次。1.源码安装:httpd(httpd依赖于两个软件)源代码安装步骤:确认源代码编译环境需安装支持 C/C++程序语言的 编译器解包 —— tar解包、释放出源代码文件配置 —— ./configure针对当前系统、软件环境,配置好安装参数编译 —— make将源代码文件变为二进制的可执行程序安装 —— make install将编译好的程序文件复制到系统中先通过sftp命令在windows命令上将文件传到linux中将文件解压缩至指定目录先创建_linux安装ps命令

pandas入门:Series、DataFrame、Index基本操作都有了!-程序员宅基地

文章浏览阅读1.7w次,点赞13次,收藏157次。导读:pandas是一款开放源码的BSD许可的Python库。它基于NumPy创建,为Python编程语言提供了高性能的、易于使用的数据结构和数据分析工具。pandas应用领域广泛,包括..._dataframe index

前端组件化之路-程序员宅基地

文章浏览阅读74次。前言: 最近在工作中发现我们前端没有一个系统的体系去支撑,产生很多不必要的代码冗余,一些公共的视图模块重复开发,导致开发效率下降,最让我郁闷的是,前端开发好的静态页面给java开发去做业务逻辑处理,会彻底的把我们开发的页面结构改的惨目忍睹。这样的形式做项目在不去想点子优化做法,真不是个合格的程序员。组件化形态: 组件化形态很多种,我个人理解为2个分支:..._纯静态前端页面如何做组件化

随便推点

vscode中如何切换Anaconda环境_vscode 连接到conda环境-程序员宅基地

文章浏览阅读3.7k次,点赞4次,收藏3次。点击左下角,然后选择环境_vscode 连接到conda环境

[转]集中式日志系统 ELK 协议栈详解-程序员宅基地

文章浏览阅读100次。为什么80%的码农都做不了架构师?>>> ..._lumberjack协议

html5图片浮动float,css 浮动(float)页面布局(下)-程序员宅基地

文章浏览阅读521次。【第七步 内容左侧板块(ContentL)布局】我们分析一下他的结构,主要包括标题和文章内容两块,并且标题和内容之间有一条虚线,而第二篇文章的内容部分是图片和文字相结合且文字环绕图片。好~!既然搞清楚结构了,后面我们布局就容易了我打算标题用标签,为什么这么用呢,原因如下第一:标签本身字体就是加粗的这样CSS里面就不用再定义字体粗细第二:如果标题用的话,搜索引擎会首先抓取里面的内容,然后提取关键词,..._html 图片float

Newcoder 148 A.Rikka with Lowbit(水~)-程序员宅基地

文章浏览阅读254次。Description给出一个长度为nnn的序列,定义操作f(x)f(x)f(x),0.50.50.5概率把f(x)=x−lowbit(x)f(x)=x-lowbit(x)f(x)=x−lowbit(x),0.50.50.5概率f(x)=x+lowbit(x)f(x)=x+lowbit(x)f(x)=x+lowbit(x),现在对这个序列做mmm次操作,操作有两种:1&nbsp;L&nbsp;..._rikka with lowbit

NSOperationQueue、NSRunLoop和线程安全_nsoperationqueue 卡主线程-程序员宅基地

文章浏览阅读550次。目前在 iOS 和 OS X 中有两套先进的同步 API 可供我们使用:NSOperation 和 GCD 。其中 GCD 是基于 C 的底层的 API ,而 NSOperation 则是 GCD 实现的 Objective-C API。 虽然 NSOperation 是基于 GCD 实现的, 但是并不意味着它是一个 GCD 的 “dumbed-down” 版本, 相反,我们可以用NSOperat_nsoperationqueue 卡主线程

目标跟踪算法五:MDNet: Learning Multi-Domain Convolutional Neural Networks for Visual Tracking_multi-domain spanning neural network-程序员宅基地

文章浏览阅读1.8w次,点赞3次,收藏34次。目标跟踪算法五:MDNet: Learning Multi-Domain Convolutional Neural Networks for Visual Tracking_multi-domain spanning neural network