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

智能推荐

机器学习之感知器和线性回归、逻辑回归以及SVM的相互对比-程序员宅基地

文章浏览阅读552次。线性回归是回归模型感知器、逻辑回归以及SVM是分类模型线性回归:f(x)=wx+b感知器:f(x)=sign(wx+b)其中sign是个符号函数,若wx+b>=0取+1,若wx+b<0取-1它的学习策略是最小化误分类点到超平面的距离,逻辑回归:f(x)=sigmoid(wx+b)取值范围在0-1之间。感知器和SVM的对比:它俩都是用于分类的模型,且都以s..._逻辑函数与svm、感知机区别和联系

webpack 理解 babel-polyfill 和 babel-runtime 及 babel-plugin-transform-runtime的配置-程序员宅基地

文章浏览阅读2.8k次,点赞2次,收藏4次。一:理解 babel之配置文件.babelrc 基本配置项 1. 什么是babel? 它是干什么用的? ES6是2015年发布的下一代javascript语言标准,它引入了新的语法和API,使我们编写js代码更加得心应手,比如class,let,for...of promise等等这..._8004 silly decomposeactions finalize babel-plugin-transform-es2015-block-sco

uni-app小程序,实现根据中文首字母排序功能_uniapp js-pinyin-程序员宅基地

文章浏览阅读4.2k次,点赞7次,收藏17次。描述:从后端调用接口获取所有热的姓名,将这些名字的首字母排序,然后放到对应字母下面,最终效果图如下:实现过程**总体实现的思路是:**首先调用接口,获取所有员工的姓名以及其他信息,将获取回来的中文名字转换为拼音,这里做的是转为姓名首字母大写的简写格式(比如:“张三” 转为“ZS”)这里只需要名字的第一个字的首字母,使用js的截取功能就能实现,中文转拼音这里我使用的是js-pinyin,将转换好的内容渲染到页面上。1、下载js-pinyin包npm install js-pinyin2、在mai_uniapp js-pinyin

windows 10 更新后无法使用远程桌面_remote desktop is available for these editions:-程序员宅基地

文章浏览阅读1w次。远程桌面部分服务器可以连接错误消息An authentication error has occurred. The function requested is not supported This could be due to CredSSP encryption oracle remediation. For more information, see https://go.m..._remote desktop is available for these editions:

黑马程序员_JAVA_反射-程序员宅基地

文章浏览阅读358次。一、反射技术 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类中的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。反射就是把Java类中的各种成分映射成相应的java类。二、Class类所有的类文件都有共同属性,所以可以向上抽取,把这些共性内容封装

SVG 保姆级入门知识详解,一篇文章带你上手!-程序员宅基地

文章浏览阅读2.1k次,点赞6次,收藏27次。SVG,即可缩放矢量图形(Scalable Vector Graphics),是一种基于 XML 的矢量图形格式,用于描述二维图形和动画。相比于基于位图的图像格式,如 PNG 和 JPEG,SVG 图像可以无限放大或缩小且不会失真。这篇文章带你了解一下SVG的魅力吧。_svg

随便推点

领扣LintCode算法问题答案-488. 快乐数_488 。 。 。 8 。 872552554545422225425225555255555417-程序员宅基地

文章浏览阅读815次。领扣LintCode算法问题答案-488. 快乐数目录488. 快乐数题解鸣谢488. 快乐数写一个算法来判断一个数是不是"快乐数"。一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是无限循环但始终变不到1。如果可以变为1,那么这个数就是快乐数。样例 1:输入:19输出:true说明:19是一个快乐的数字1 ^ 2 + 9 ^ 2 = 828 ^ 2 + 2 ^ 2 = 686 ^ 2 + 8 ^ ._488 。 。 。 8 。 87255255454542222542522555525555541774。 ,:,。冫、、丶

Memory Model -- 06 -- 运行时数据区(五、方法区)_java内存模型5大块-程序员宅基地

文章浏览阅读189次。一、方法区 (Method Area)方法区 (Method Area) 与 Java 堆一样,是各个线程共享的内存区域,用于存储已经被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据当方法区无法满足内存分配的需求时,将会抛出 OutOfMemoryError 异常二、永久代与元空间在 Java 虚拟机规范中,只规定了方法区的概念及其作用,但并没有规..._java内存模型5大块

JAVA实验六_用java模拟向货船上装集装箱-程序员宅基地

文章浏览阅读1.4k次,点赞6次,收藏14次。JAVA实验六实验六一共四题,附上题目及完整代码。8702题目內容:建立Person类,成员变量为姓名和年龄,具有构造方法、get/set方法。创建NoAgesException类,当年龄为负数或大于200岁抛出异常IllegalArgumentException,正常输出“姓名年…龄从”,键盘输入姓名和年龄建立Person对象,测试该对象。输入输出说明:张三 300年龄数值非法李四 77李四…77代码编辑:import java.util.Scanner;class NoAges._用java模拟向货船上装集装箱

“不念过往,不畏将来”——2017年山东省第八届ACM大学生程序设计竞赛总结_2017年山东省acm程序设计大赛-程序员宅基地

文章浏览阅读627次。不念过往,不畏将来今天去参加了第八届山东ACM省赛,也是自己第一次参加正式的ACM比赛,有诸多感想。先说说去比赛的经过吧,整个大体上还是比较顺利的,青科大的志愿者也十分的负责用心(排队排的很有意思),住宿环境也还不错,但是宾馆的隔音的效果实在是有一点差,第二天比赛还算是清醒,迅速进入了状态,我们队还算顺利的A掉了I,G两个水题,然后开了两道题,一开始读错题导致错了两次,但是还好及时发现,A_2017年山东省acm程序设计大赛

IRC_tcp服务器支持irc-程序员宅基地

文章浏览阅读1.2k次。转载自 mst_beach 最终编辑 mst_beach IRC(Internet Relay Chat的缩写,“因特网中继聊天”)是一种通过网络的即时聊天方式。其主要用于群体聊天,但同样也可以用于个人对个人的聊天。 芬兰人雅尔口·欧伊卡林恁(Jarkko Oikarinen)于1988年8月创造了IRC来取代一个叫做MUT的程序。 连接方法 以连接到 FreeNode (chat.freenode.net) 上的 #wikipedia-zh 聊天室为例: 在支持 IRC 协议的浏览器地址栏中输_tcp服务器支持irc

特殊教育学校计算机教学计划,特殊教育学校七年级环境教育教学计划.doc-程序员宅基地

文章浏览阅读106次。特殊教育学校七年级环境教育教学计划特殊教育学校七年级环境教育教学计划李红榜◆学生情况分析:七年级共有学生10人,学生有一定的环保意识和环保知识,但不系统、不全面。极少开展综合实践活动。通过本册教材的学习,使他们掌握环保的有关知识,通过开展大量的实践活动,做环保的小主人。◆教学总目标1、学生了解一些生态环境问题的产生和发展,感知这些环境问题带来的危害,树立环保意识,转变浪费资源、破环环境的生活方式。...