Android常用布局

news2024/10/12 4:22:45

目录

布局文件中常见的属性

1. 基本布局属性

1)android:layout_width

2)android:layout_height

3)android:layout_margin

4)android:padding

2. 线性布局 (LinearLayout) 属性

1)android:orientation

2)android:gravity

3)android:layout_weight

4)案例LinearLayout水平布局

1.代码

2.效果

案例:LinearLayout垂直布局

1.代码

2.效果

3. 相对布局 (RelativeLayout) 属性

1)android:layout_alignParentTop/...Bottom/...Left/...Right

2)android:layout_centerInParent

3)android:layout_above/...below/...toLeftOf/...toRightOf

4)android:layout_alignWithParentIfMissing

案例:RelativeLayout相对布局的简单使用

1.代码

2.效果

4. 帧布局 (FrameLayout) 属性

1)android:foreground

2)android:layout_gravity

案例:帧布局的简单使用

1.代码

2.效果

5. 表格布局 (TableLayout) 和表格行 (TableRow) 属性

1)android:collapseColumns

2)android:stretchColumns

案例:表格布局的简单使用

1.代码

2.效果

3.注意

6. 网格布局 (GridLayout) 属性

1)android:rowCount/columnCount

2)android:alignmentMode

3)android:useDefaultMargins

案例:网格布局的简单使用

1.代码

2.效果

7. 其他常用属性

1)android:background

2)android:visibility

3)android:clickable

4)android:focusable

5)android:enabled

6)android:alpha

7)android:id


布局文件中常见的属性

1. 基本布局属性

1)android:layout_width

        设置视图的宽度,可以是具体数值(如 100dp)、match_parent(与父元素一样宽)或 wrap_content(根据内容自动调整宽度)。

2)android:layout_height

        设置视图的高度,选项同上。

3)android:layout_margin

  设置视图外边距,也可以分别设置四个方向的外边距

android:layout_marginLeftandroid:layout_marginTopandroid:layout_marginRightandroid:layout_marginBottom)。

4)android:padding

  设置视图内边距,同样可以分别设置四个方向的内边距(android:paddingLeftandroid:paddingTopandroid:paddingRightandroid:paddingBottom)。

2. 线性布局 (LinearLayout) 属性

1)android:orientation

        设置子元素的排列方向,值为 vertical 或 horizontal

2)android:gravity

        设置内容相对于视图的对齐方式。

3)android:layout_weight

        分配剩余空间给子视图的比例权重。

4)案例LinearLayout水平布局

1.代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--水平布局-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="buttonOne"

        />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="buttonTwo"

            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="buttonThree"

            />




    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

2.效果

案例:LinearLayout垂直布局

1.代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Three"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Four"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Five"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Six"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

2.效果

3. 相对布局 (RelativeLayout) 属性

1)android:layout_alignParentTop/...Bottom/...Left/...Right

        将视图对齐到父容器的顶部/底部/左边/右边。

2)android:layout_centerInParent

        将视图居中于父容器

3)android:layout_above/...below/...toLeftOf/...toRightOf

        相对于另一个视图定位

4)android:layout_alignWithParentIfMissing

        如果指定的兄弟视图不存在,则与父视图对齐。

案例:RelativeLayout相对布局的简单使用

1.代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--相对布局-->
    
    <!--按钮位于左上角-->
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="lefTop" 
        
        android:layout_alignParentLeft="true"
        
        />
    
    <!--按钮位于右上角-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rightTOp"

        android:layout_alignParentRight="true"
        
        
        />
    <!--按钮上中-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="topCenter"

        android:layout_centerHorizontal="true"


        />
    <!--按钮左中-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="leftCenter"

        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
       
        />
    
    <!--myCenter上方-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_above="@+id/myCenter"
        android:layout_centerHorizontal="true"
        android:text="top"
        />
    
    
    <!--按钮居中-->
    <Button
        android:id="@+id/myCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        
        android:layout_centerInParent="true"
        

        />
    <!--myCenter下方-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/myCenter"
        android:layout_centerHorizontal="true"
        android:text="below"
        />
    
    
    <!--按钮右中-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rightCenter"

        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"

        />
    <!--按钮在左下脚-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="leftButtom"

        android:layout_alignParentBottom="true"
        
        />
    <!--按钮在有下角-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rightButtom"
        
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"

        />
    
    <!--按钮下中-->

    <!--按钮左中-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="buttomCenter"

        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        />
</RelativeLayout>

2.效果

4. 帧布局 (FrameLayout) 属性

1)android:foreground

设置前景图像。

2)android:layout_gravity

设置视图在其父容器中的位置。

案例:帧布局的简单使用

1.代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:gravity="center">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_gravity="center"
        >
        <Button
            android:layout_width="400dp"
            android:layout_height="400dp"
            android:background="#746232"
            android:layout_gravity="center"
            android:text="20" />
        <Button
            android:layout_width="350dp"
            android:layout_height="350dp"
            android:background="#F6EAF3"
            android:layout_gravity="center"
            android:text="20" />

        <Button
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:background="#F807C4"
            android:layout_gravity="center"
            
            android:text="20" />

        <Button
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:background="#8BB3F0"
            android:layout_gravity="center"
            android:text="20" />


        <Button
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="#0AE649"
            android:layout_gravity="center"
            android:text="20" />

        <Button
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="#FF9800"
            android:layout_gravity="center"
            android:text="20" />
        
        

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#080CE2"
            android:layout_gravity="center"
            android:text="20" />

        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#E96FC6"
            android:layout_gravity="center"
            android:text="20" />

        <Button
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="#0DD8F4"
            android:layout_gravity="center"
            android:text="20" />

       

    </FrameLayout>

        
</LinearLayout>

2.效果

5. 表格布局 (TableLayout) 和表格行 (TableRow) 属性

1)android:collapseColumns

  指定要折叠的列索引。

2)android:stretchColumns

  指定应伸展以填充可用空间的列索引。

案例:表格布局的简单使用

1.代码
<?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"
    android:background="#845612"

    

    >

    <TableRow

        android:background="#145678"
        android:layout_column="4"
        >

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

            android:layout_column="0"
            />

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

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

            android:layout_column="1"
            />

    </TableRow>

    <TableRow
        android:background="#a45678"
        android:layout_column="3"
        >



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

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

            android:layout_column="1"
            />

    </TableRow>

    <TableRow
        android:background="#645678"
        android:layout_column="3"
        >

        <Button
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:text="按钮"

            android:layout_column="0"
            />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"

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

            android:layout_column="3"
            />

    </TableRow>

</TableLayout>

2.效果

3.注意

修改主题,Theme.Design.NoActionBar,不然自定义的颜色无法使用

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Design.NoActionBar"
        tools:targetApi="31" >

        <activity
            android:name=".MainActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

6. 网格布局 (GridLayout) 属性

1)android:rowCount/columnCount

定义网格的行数和列数。

2)android:alignmentMode

设置如何对齐单元格内的内容。

3)android:useDefaultMargins

是否使用默认的边距。

案例:网格布局的简单使用

1.代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="3"
        android:rowCount="3"
        android:id="@+id/myGridTable">

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="0"
            android:layout_row="0"
            android:background="#31B6C3"
            android:text="1"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="1"
            android:layout_row="0"
            android:background="#DE117C"
            android:text="2"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="2"
            android:layout_row="0"
            android:background="#08D542"
            android:text="3"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="0"
            android:layout_row="1"
            android:background="#4B07EB"
            android:text="4"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="1"
            android:layout_row="1"
            android:background="#EB07A7"
            android:text="5"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="2"
            android:layout_row="1"
            android:background="#EB7A07"
            android:text="6"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="0"
            android:layout_row="2"
            android:background="#07EB8A"
            android:text="7"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="1"
            android:layout_row="2"
            android:background="#0EFDD9"
            android:text="8"
            android:gravity="center"
            android:layout_weight="1" />

        <TextView
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_column="2"
            android:layout_row="2"
            android:background="#FF0000"
            android:text="9"
            android:gravity="center"
            android:layout_weight="1" />

    </GridLayout>
</RelativeLayout>

2.效果

7. 其他常用属性

1)android:background

设置背景颜色或图片

2)android:visibility

设置视图的可见性,值可以是 visibleinvisible 或 gone

3)android:clickable

设置视图是否可点击

4)android:focusable

设置视图是否可以获得焦点

5)android:enabled

设置视图是否启用

6)android:alpha

设置视图透明度(0.0完全透明,1.0完全不透明)。

7)android:id

为视图提供一个唯一的标识符。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2206862.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深度学习神经网络笔记--卷积神经网络

为什么要用卷积 捕捉特征&#xff0c;如文末的图&#xff09;不受位置影响&#xff08;左右&#xff0c;前后&#xff0c;上下&#xff09;可以参考下图&#xff1a;卷积操作 可移动的小窗口与图像数据逐元素相乘后相加小窗口是滤波器&#xff0c;卷积核&#xff0c;&#xff0…

毕业设计之—基于ManTra-Net的图像篡改检测方法研究与应用实现

1.摘要 随着互联网、社交媒体和简易图像操作工具的普及&#xff0c;图像篡改带来的问题日益严重。为了解决这一问题&#xff0c;研究者们利用深度卷积神经网络来检测图像篡改并定位篡改区域。为此我们训练了一个ManTra-Net模型&#xff0c;该模型以TensorFlow为后端&#xff0c…

什么是网络安全等级保护?企业如何建立安全系统?一篇带你快速了解→:

等保测评的定义与目的 等保测评旨在评估信息系统的安全性&#xff0c;并根据评估结果给予相应的安全等级。该等级反映了信息系统在保护国家安全、经济安全、社会公共利益以及个人合法权益方面的能力。通过等保测评&#xff0c;可以确保信息系统符合国家法律法规的要求&#xf…

DSP CMD文件使用

背景描述: 在CCS编译代码时出现如下警告 解决方法: 找到cmd文件(这里是用的系统自动生成的)&#xff0c;在Section部分找到对应的核 #ifdef CORE7.text > CORE7_L2_SRAM.stack > CORE7_L2_SRAM.bss > CORE7_L2_SRAM.cio &g…

(28)QPSK调制信号使用矩形脉冲成形的误符号率和误比特率MATLAB仿真

文章目录 前言一、系统模型说明二、MATLAB仿真代码三、MATLAB仿真结果四、仿真结果分析 前言 在QPSK通信系统仿真时&#xff0c;经常会加入调制信号的脉冲成形模块&#xff0c;本文将讨论在这种情况下信道的信噪比该如何设置&#xff0c;并给出MATLAB仿真代码&#xff0c;画出…

滑动窗口_⽔果成篮找到字符串中所有字⺟异位词

⽔果成篮 904. 水果成篮 - 力扣&#xff08;LeetCode&#xff09; 相当于求数字种类不超过2的最长字字符串 我们先看一看例4.从第一个元素开始最长字符串3331&#xff0c;下一次从第二个位置数吗&#xff1f;没必要&#xff0c;因为只有当字符串中数字种类变为1时&#xff0c;…

库的相关使用

1.1 库的概念 库是由.c文件编译生成的二进制文件。 库的内部就是各种函数的实现。 windows中库的格式&#xff1a; xxx.dll -- 动态库 xxx.lib -- 静态库 linux中库的格式&#xff1a; libxxx.a --- 静态库 libxxx.so --- 动态库 2.1 静态库的制作和使用 2.1.1 静态库的…

9.2分新剧教你如何面对生活的苦涩与温暖

如果你最近在寻找一部既温暖治愈&#xff0c;又能引发思考的好剧&#xff0c;《住宅区的两人》无疑是个不错的选择。虽然没有大起大落的情节&#xff0c;但它却用温柔的叙事方式和细腻的情感刻画赢得了观众的心&#xff0c;目前在豆瓣上拿下了9.2的高分。这部剧带给人的不仅仅是…

图片懒加载(lazyload )

图片懒加载 懒加载&#xff08;Lazy Loading&#xff09;是一种计算机编程技术&#xff0c;用于延迟初始化对象或资源&#xff0c;直到它们实际需要使用时才进行加载或初始化。这种技术可以提高程序的启动速度和性能&#xff0c;减少不必要的资源消耗&#xff0c;特别是在处理…

基于头脑风暴优化的模糊PI控制系统simulink建模与仿真

目录 1.课题概述 2.系统仿真结果 3.核心程序与模型 4.系统原理简介 5.完整工程文件 1.课题概述 头脑风暴优化&#xff08;Brain Storm Optimization, BSO&#xff09;是一种受人类集体创新过程启发的群体智能算法。它通过模拟团队成员之间的信息交流和想法生成来寻找最优解…

【含开题报告+文档+PPT+源码】基于SpringBoot+Vue医药知识学习与分享平台的设计与实现

开题报告 本论文介绍了一个名为岐黄之家的知识学习与分享平台的设计与实现。该平台旨在为用户提供一个交流、学习和分享医药知识的空间。论文首先介绍了中医院交流平台的背景和相关研究现状。随着互联网的快速发展&#xff0c;中医学的学习和交流需求逐渐增多&#xff0c;因此…

77.【C语言】EOF的解释

1.cplusplus网的介绍 在这几篇文章提到过,但没有详细阐释过EOF的细节 24.【C语言】getchar putchar的使用E4.【C语言】练习&#xff1a;while和getchar的理解32.【C语言】详解scanf 75.【C语言】文件操作(3) cplusplus网的介绍 点我跳转 翻译 常量 EOF 文件结束(End-Of-Fi…

新版Win32高级编程教程-学习笔记01:应用程序分类

互联网行业 算法研发工程师 目录 新版Win32高级编程教程-学习笔记01&#xff1a;应用程序分类 控制台程序 强烈注意 窗口程序 启动项 程序入口函数 库程序 静态库 动态库程序 几种应用程序的区别 控制台程序 本身没有窗口&#xff0c;其中的doc窗口&#xff0c;是管…

大数据之——VWare、Ubuntu、CentOs、Hadoop安装配置

前言&#xff1a;这里很抱歉前几期考研专题以及PyTorch这些内容都没有更新&#xff0c;并不是没有在学了&#xff0c;而是事太鸡儿多了&#xff0c;前不久刚刚打完华为开发者比赛&#xff0c;然后有紧接着高数比赛、考研复习&#xff0c;因此这些后续文章都在草稿状态中&#x…

yolov8-melodic-cam-anconda环境配置及目标检测

1、基础环境安装 安装配置cuda、Anconda等环境&#xff0c;具体安装参考如下&#xff1a; https://blog.csdn.net/weixin_45702256/article/details/142555187 2、torch安装 下载链接&#xff1a;https://pytorch.org/ 根据配置下载对应版本&#xff0c;CUDA11.4 可用11.3下…

【c数据结构】队列详解!(模拟实现、OJ练习实操)

队列的概念 队列就像排队&#xff0c;先进先出&#xff0c;zz先到先得&#xff08;队头的人先出去&#xff0c;队尾的人排在最后出去&#xff09; 对比栈 队列示意图 概念&#xff1a;只允许在⼀端进⾏插⼊数据操作&#xff0c;在另⼀端进⾏删除数据操作的特殊线性表&#xff…

68 Netty

68 Netty 参考资料 【硬核】肝了一月的Netty知识点 概念 Netty 是一个高性能、异步事件驱动的网络应用框架&#xff0c;简化了 Java 网络编程&#xff0c;适用于构建高效、可扩展的网络服务器和客户端。 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架&#xff0c;使…

访问远程桌面或共享文件夹,输入正确凭证,但提示登录没有成功或者用户名密码不正确

可以在目标机器试试以下方法&#xff1a; winR 打开 "gpedit.msc" 本地组策略编辑器&#xff0c;导航到 计算机配置 > Widnows 设置 > 安全设置 > 本地策略 > 安全选项 找到 网络访问&#xff1a;本地账户的共享和安全模型&#xff0c;把 仅来宾 改为 …

介绍各种编程语言

记得点个赞再看哦 常见的编程语言 在当今的计算机编程领域&#xff0c;有许多种编程语言&#xff0c;以下是一些常见的编程语言&#xff1a; Python&#xff1a;是一种代表简单思想的语言&#xff0c;具有极其简单的语法&#xff0c;是FLOSS&#xff08;自由/开放源码软件&…

简单解析由于找不到xinput1_3.dll,无法继续执行代码的详细解决方法

电脑上突然跳出“由于找不到xinput1_3.dll&#xff0c;无法继续执行代码”的提示&#xff0c;这着实令人心烦&#xff0c;特别是当你正着急使用相关软件或者程序的时候。别担心&#xff0c;其实有五种科学有效的解决办法。大家得清楚&#xff0c;xinput1_3.dll是一个在众多软件…