Android studio 六大基本布局详解_android studio布局_AaVictory.的博客-程序员宅基地

技术标签: 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

智能推荐

什么是HTTP,HTTP 与 HTTPS 的区别_http视_棉花糖one.的博客-程序员宅基地

HTTP 是一个在计算机世界里专门在两点之间传输文字、图片、音频、视频等超文本数据的约定和规范区别HTTPHTTPS协议运行在 TCP 之上,明文传输,客户端与服务器端都无法验证对方的身份身披 SSL( Secure Socket Layer )外壳的 HTTP,运行于 SSL 上,SSL 运行于 TCP 之上, 是添加了加密和认证机制的 HTTP。端口80443资源消耗较少由于加解密处理,会消耗更多的 CPU 和内存资源开销无需证书需要证书,而证书

装饰者模式_装饰着者模式_changaspl的博客-程序员宅基地

装饰者模式​ 装饰模式(Decorator Pattern) :动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活。其别名也可以称为包装器(Wrapper),与适配器模式的别名相同,但它们适用于不同的场合。根据翻译的不同,装饰模式也有人称之为“油漆工模式”,它是一种对象结构型模式。装饰模式包含如下角色:• Compone...

java中获取路径中的空格处理(%20)问题_lj830723的博客-程序员宅基地

在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误,解决办法如下:[code=&quot;java&quot;]String path=Parameter.class.getResource(&quot;&quot;).getPath();//得到路径//String path=Parameter.class.getResource(&quot;&quot;).toString();/...

蓝牙耳机都会有底噪吗?300左右哪款蓝牙耳机音质好?_momo1996_233的博客-程序员宅基地

由于手机上耳机插孔的取消,让大家开始使用蓝牙耳机。在便携性上,蓝牙耳机有着无与伦比的优势,不过,也有小伙伴反应,自己购买的蓝牙耳机有底噪,这是什么原因呢?是蓝牙耳机都会有底噪吗?【首先来说说什么是底噪?】一般的说法就是放音乐时,把声音调到最低音量,你能听到耳机里有沙沙声,或者音乐暂停时有几秒沙沙声然后消失,手机上打开一些应用也会有声音,这种情况,我们一般称之为“底噪”。【如何消除蓝牙耳机底噪?】1、无法彻底消除,蓝牙耳机的低噪是由于蓝牙无线信号传输的过程中受到干扰影响产生的,是蓝牙耳机普遍存

哈鲁-怂_东躲西藏的西城的博客-程序员宅基地

Halu,男,2015年12月9日出生于河南郑州。貌似天不怕地不怕,但其实只限第二+N次。每个第一次,都是怂字当先。也许跟当初犬舍拍小视频时的口音有关,抓起来,拍5秒,然后“松”,就完成。但河南话的“松”=普通话的“怂”。 于是那个怂我听了百八十遍,我懂了,halu也懂了。 从他来到我身边,每一个第一次,好听了叫谨慎,实质就是“怂”。这些第一次包括:狗窝,毛巾,玩具(会响的),磨牙的(掉在...

简述De-noising auto-encoder 和variation auto encoder的区别?_折竹丶的博客-程序员宅基地

简述De-noising auto-encoder和variation auto encoder的区别?De-noising auto-encoder是在input data引入噪声,并使得output data和input data越接近越好。variation auto encoder是在input data经过降维得到的隐藏层引入噪声,且限制噪声的方差不能太小并使得output data和input data越接近越好。...

随便推点

js基础3衣服相册_xwzj的小白的博客-程序员宅基地

衣服相册&lt;!DOCTYPE html&gt;&lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;衣服相册&lt;/title&gt; &lt;style type="text/css"&gt; *{ padding: 0; margin: 0; } ul{ list-style: none; overflow: hidden; } ul l

ArcGIS制图及出图小技巧——以土地利用图为例_arcgis出图要一个局部特写图_ceibake的博客-程序员宅基地

目录:一、关于地图配色1、使用取色器2、批量修改符号二、关于出图1、快速调整数据框以适应纸张大小2、使用导入符号系统,让每年的土地类型颜色一致3、快速调整多个数据框的大小4、修改图例1)去除图例中的多余文字2)图例多列显示3)修改图例中各类显示名称4)只显示当前视图中的类别一、关于地图配色1、使用取色器看别人做的图配...

如何退出函数时再做一些固定的事情(比如释放内存)--scopeguard_步步为赢567的博客-程序员宅基地

socope.h文件:#ifndef scopeguard_h__#define scopeguard_h__namespace smn{ template class ScopeGuard { Fun f_; bool active_; public: ScopeGuard(Fun f): f_(st

什么叫生命周期 并且举例_JZD-X的博客-程序员宅基地

Servlet 生命周期1 Servlet 生命周期Servlet 创建的时机:[ 默认 ] 用户第 1 次发送请求的时候,由 Web 容器实例化 Servlet 对象 也可以在 web.xml 中配置服务器启动时即创建 Servlet 2 Servlet 接口中生命周期方法每个 Servlet 在 tomcat 中只实例化一次,只会存在一个对象。...

动态规划实现子数组和的最大值_baichui5310的博客-程序员宅基地

/**[email protected] 转载请注明出处*问题:输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。*求所有子数组的和的最大值。要求时间负责度为O(n)。*使用动态规划方法来实现:*如果用函数f(i)表示以第i个数字结尾的子数组的最大和,那么我们需要求出max(f[0...n])。*我们可以给出如下递归公式求f...

维护序列(线段树,区间加,区间乘)_m悟空的博客-程序员宅基地

题目描述原题来自:AHOI 2009老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。有长为的数列,不妨设为。有如下三种操作形式:把数列中的一段数全部乘一个值; 把数列中的一段数全部加一个值; 询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模 P的值。输入格式第一行两个整数 n和 P;第二行含有 n个非负整数,从左到右依次为 ai ;...

推荐文章

热门文章

相关标签