Android_studio布局

Android Studio 的六种基本布局 开发者可以创建自定义的约束类,为ConstraintLayout添加新的布局功能。 阅读详情

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

  • 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
  • 帧布局(FrameLayout):组件从屏幕左上方布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  •  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
1. 线性布局

线性布局是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.administrator.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮2" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right"
            >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>


2. 帧布局
帧布局是从屏幕的左上角(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="380dp"
        android:layout_height="380dp"
        android:background="#ff00ff"
        android:layout_gravity="center"
        />

        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="center"
            android:background="#00ff00"
            />
            <ImageView
                android:layout_width="320dp"
                android:layout_height="320dp"
                android:background="#0000ff"
                android:layout_gravity="center"
                />
        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:background="#FFFF6600"
            android:layout_gravity="center"
        />
        <ImageView
            android:layout_width="280dp"
            android:layout_height="280dp"
            android:background="#FF00FFD9"
            android:layout_gravity="center"
        />

</FrameLayout>


3.表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
结果如下:
实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="这是第一个lay的第一行"
        />
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#ff0000"
                android:text="2行二1列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:text="2行二2列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:layout_weight="1"
                android:text="2行二3列"
                />
        </TableRow>
</TableLayout>
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1"
        >
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第1列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第2列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第3列"
                />
        </TableRow>
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行1列"
                />
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行2列"
                />
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行3列"
                />
        </TableRow>
</TableLayout>
</TableLayout>
4.相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
基本用法都在图片中:

结果如下:

原代码如下:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:id="@+id/tv_main_blue"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_toRightOf="@id/tv_main_blue"
        android:layout_below="@id/tv_main_blue"
        android:id="@+id/tv_main_bule2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_toRightOf="@id/tv_main_bule2"

        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:id="@+id/tv_main_blue4"
        android:layout_below="@id/tv_main_bule2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:id="@+id/tv_main_blue5"
        android:layout_below="@id/tv_main_bule2"
        android:layout_toRightOf="@id/tv_main_bule2"
        />
</RelativeLayout>

5. 绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。


Android Studio开发 之 “简单布局” 1.View视图2.视图在“XML布局文件”中常用的属性定义说明3.视图在“代码”中常用的设置方法说明 4.ViewGroup视图组 阅读详情

相关推荐

Android Studio代码笔记08.帧布局和约束布局

帧布局 最简单的布局,动画中较常用 都堆到左上角 示例代码 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/re...

weixin_44664660的博客 688

AndroidStudio3.2 帧式布局

- 了解帧式布局用途 - 熟悉帧式布局常用属性 - 掌握线性布局嵌套帧式布局

qq_62972842的博客 1994

Android Studio 笔记3.2 帧式布局

Android Studio 笔记3.2 帧式布局

ZQIR12的博客 690

Android Studio中设计 UI 控件全部堆叠在左上角的解决方法

UI控件全部堆叠在左上角的情况是我在把代码上传到码云之后想点开xml界面截图提交作业的时候发现的。当时整个模块的所有xml界面都如下图所示: 看见这界面的心情,目瞪口呆,好歹是在考试,能不能争点气呀,关键是这问题咱之前也没见过。 好在想想之前在真机上运行了一回,软件还在手机上,手机截图应该也差不离,也就草草交了。关键问题没解决心里膈应,所以考完后赶紧回来解决问题,这也就回归正题了。 在寻找问题的时候我百度搜索,找到传说能够解决 UI 控件全部堆叠在左上角的以下几种方法,不过是不是真的能解决我也不知

duo le的博客 8628

Android Studio | 页面布局

1、相对布局——RelativeLayout

qq_45337960的博客 4570

Android studio-约束布局

约束布局

fjnu_chen的博客 2271

Android studio实现层级式导航

在应用内导航有几种方式,其中有临时性导航,层级式导航等等,在此,我阐述一下层级式导航的工作原理以及实现层级导航(hierarchical navigation)可在应用内逐级向上导航。首先我们需要启用应用图标向上导航按钮的功能,并在Fragment视图上显示向左的图标,那么需要调用Fragment的DisplayHomeAsUpEnabled属性 setDisplayHomeAsUpEnabled...

GGHS_up的博客 2206

安卓开发Android studio学习笔记06:常用布局 - 相对布局

【代码】安卓开发Android studio学习笔记06:常用布局 - 相对布局。

那人独钓寒江雪 426

Android Studio帧式布局的介绍

Android Studio帧式布局的介绍。

RainBow_xiy的博客 720

tcllib2.0 package - csv

tcl lib 的csv库

shin 859

解决PADs VX2.7安装中的License失效与软件卡死问题

本文详细解析了PADs VX2.7安装过程中常见的License失效与软件卡死问题,提供了从破解工具选择、环境变量设置到输入法兼容性调整等实用解决方案。特别推荐使用Siemens EDA系列新版破解工具,并分享性能调优与定期维护技巧,帮助工程师高效解决安装难题。

weixin_29234239的博客 813

Android Studio中让项目的包名分层次结构进行显示

背景 设置方法 A little bit of progress every day!Come on! 背景 项目有时候需要命名不同的包,这时候分层次结构显示包名就变得很有用处了。下面这个小技巧跟着我来一起设置吧! 设置方法 刚创建完的项目打开包名显示如下: 然后点击下面的选项设置项可以设置包名,如图所示: 弹出的界面如下: 这里可...

活在当下!输出快乐! 1万+

ABB机器人常见的几大数据类型

ABB机器人数据存储描述了机器人控制器内部的各项属性,ABB机器人控制器数据类型多达100余种,其中常见的数据类型包括基本数据、i/o数据、运动相关数据。 1、基本数据 基本数据: bool 逻辑值:逻辑状态下赋予的真或者假。逻辑值有两种情况:成立和不成立则逻辑值为真使用true或1表示不成立则逻辑值为假使用false或0表示 byte 字节值:用于计量存储容量的一种计量单位,取值范围为(0-255) num 数值:变量、可存储整数或小数整数取值范围(-8388607~8388608) dnum

soinlove36的专栏 1万+

Android Studio:线性布局

安卓学习:线性布局

m0_62491877的博客 2190

基于 parallels ubuntu 20.04 LTS (focal) 的ros+px4+gazebo 安装

基于 parallels ubuntu 20.04 LTS (focal) 的ros+px4+gazebo 安装。

weixin_40132006的博客 2240

1、VMware vSphere 全面指南:从入门到精通

本文全面介绍了VMware vSphere从入门到精通的完整知识体系,涵盖基础概念、环境适配、部署规划、安装配置、网络与存储准备、虚拟机创建与管理、资源组织、事件监控、巡检流程、主机配置文件管理、资源池优化、高可用性与容错应用,以及故障排除方法。结合实用工具和部署技巧,帮助读者系统掌握vSphere的核心功能与运维实践,提升虚拟化环境的稳定性与管理效率。

pz890123的博客 94

安卓学习 布局篇 Android studio

安卓学习布局篇(一) LinearLayout布局 orientation 布局中组件的排列方式 gravity 控制组件所包含的子元素的对齐方式,,可多个组合 layout_gravity控制该组件在父容器里的对其方式 background为该组件设置一个背景图片,,或者是直接用颜色覆盖 divider分割线 showDividers设置分割线所在的位置,none(无),beginning(开始),end(结束),middle(每两个组件间) dividerPadding设置分割线的padding la

qq_45059975的博客 2067

Qt C++ HTTP 调用通义千问大模型 X 学术论文润色工具(SCI 格式适配)

请求地址:`https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation`;| Windows | 打包时携带 `Qt6Network.dll`/`libcrypto-1_1-x64.dll` 等依赖;| Linux | 安装 `libssl-dev` 和 `libcurl4-openssl-dev`;| API 调用失败 | 检查 API-KEY 是否有效;

寒寒豆豆软件公司的博客 104

android获取其他app布局,Android Studio查看其它APP的布局结构

概述日常使用别家的APP过程中,会遇到一些比较好看的布局,这时候我们就想学习一下别人的布局结构,以便参考。(1)手机连接电脑、设置手机为USB调试模式(2)运行Android Studio,打开 Tools--Android--Android Device Monitor注意:android Studio3.1开始只能通过命令行的方式启动Android Device Monitor。或者手动打开文...

weixin_34822215的博客 4798

Qt应用软件【协议篇】自定义协议处理串口半包贴包

Qt串口数据,半包,贴包,丢包数据处理

小鱼酱的博客 1211

Golang内存管理:GC调优实战案例

在Golang编程中,内存管理是一个非常重要的方面。良好的内存管理可以提高程序的性能和稳定性,而GC(垃圾回收)调优则是优化内存管理的关键手段。本文的目的就是通过实际案例,深入讲解Golang中GC调优的方法和技巧,让读者能够在实际项目中更好地进行内存管理和性能优化。范围涵盖了从核心概念的解释到具体代码实现和调优过程的详细分析。本文首先会介绍一些相关的术语和核心概念,让读者对Golang内存管理和GC有一个基本的认识。然后通过故事引入的方式,更加形象地解释核心概念及其相互关系。

AI Agent Harness Engineering 1118

Android studio线性布局、表格布局、相对布局、帧布局

线性布局 .xml文件代码如下: &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt; &amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;m

qq_41218412的博客 4356

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

安卓应用开发——Android Studio中滚动布局的应用

现如今,许多软件的主界面都采用了滚动条的功能,以展示软件中长条的商品又或是广告,经典的社交软件与购物软件等软件都不可避免的需要实现这一功能,今天我们来简单学习在AndroidStudio中实现一个属于自己的滚动布局页面。

2303_76399326的博客 1346

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

android studio 线性布局,及布局嵌套

android studio 新建一个项目,修改XML如下 注意此项目的Activity名称为:MyLinearlayout。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools

yonglwin的博客 3046

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Android Studio 中各种UI布局的介绍

本文主要介绍了 Android Studio 中常见的UI布局。LinearLayout 会按照水平或者垂直方向依次排列子视图。通过 android:orientation 属性可以设定排列方向,取值为 horizontal(水平)或者 vertical(垂直)。RelativeLayout 是 Android 开发中常用的布局方式,它允许子视图根据其他视图或父布局的位置来确定自身位置。

gishsjsnsn的博客 1226
上一篇: 设计模式
下一篇: Android 中常见控件的使用
殇丶
博客等级 码龄10年 35粉丝 46原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值