Android资源类型与常用的四种布局资源

一、Android 资源类型总览

Android应用资源存放在 res/ 目录下,编译时系统会自动生成 R 类索引。主要资源类型如下:

资源目录 资源类型 说明
drawable/ 图片/图形资源 存放PNG、JPG、矢量图、形状资源等
layout/ 布局资源 定义界面结构和控件摆放规则
values/ 值资源 字符串、颜色、尺寸、样式、数组、布尔值等
mipmap/ 启动图标 存放不同分辨率的应用启动图标
anim/ 补间动画 定义透明度、缩放、平移、旋转动画
animator/ 属性动画 高级动画,控制控件属性变化
menu/ 菜单资源 定义选项菜单、上下文菜单
raw/ 原生资源 存放音频、视频等原始文件,不编译
xml/ 自定义XML 任意XML配置文件
font/ 字体资源 存放TTF/OTF自定义字体文件
color/ 颜色状态列表 按钮不同状态(按下、选中、禁用)的颜色切换
interpolator/ 插值器资源 控制动画速度变化(如先快后慢)
transition/ 转场动画 Activity/Fragment切换动画

二、布局的通用属性

1. android:id

用于设置当前布局的唯一标识。 在XML文件中它的属性值是通过"@+id/属性名称"定义。

2. android:layout_width

用于设置当前布局的宽度,其值可以是具体尺寸也可以是系统定义的值。

  • fill_parent:表示该布局的宽度与父容器的宽度相同。
  • match_parent:与fill_parent作用相同,从Android2.2.0开始推荐使用match_parent。
  • wrap_content:表示该布局的宽度使用该布局恰好能包裹它的内容。

3. android:layout_height

用于设置当前布局的高度,其值可以是具体尺寸也可以是系统定义的值。

  • fill_parent:表示该布局的高度与父容器的高度相同。
  • match_parent:与fill_parent作用相同,从Android2.2.0开始推荐使用match_parent。
  • wrap_content:表示该布局的高度使用该布局恰好能包裹它的内容。

4. android:background

用于设置当前布局的背景,其值可以引用图片资源,也可引用颜色资源。

5. android:layout_margin

用于设置当前布局与其父容器边界或周围其他控件 的距离。margin 是外间距 ,作用于布局外部,其属性为具体数值。

6. android:padding

用于设置布局内部内容(子控件)与布局边界 的距离。padding 是内间距 ,作用于布局内部,其值可以是具体尺寸。

三、常用的四种布局资源

Android资源类型中,布局资源(layout/) 用于定义界面布局,即界面的结构和控件排列方式。常用的布局资源有以下四种:线性布局 (LinearLayout)、相对布局 (RelativeLayout)、表格布局 (TableLayout)、帧布局(FrameLayout)。

1.线性布局(LinearLayout)

特点:将子控件按照水平(horizontal)垂直(vertical) 方向依次排列。

核心属性:

android:orientation:设置布局内控件的排列顺序。其中vertical 表示LinearLayout内控件从上到下 依次排列;horizontal 表示LinearLayout内控件从左到右依次排列。

android:layout_weight:在布局内设置控件权重,属性值可以直接使用int值。通过设置该属性值,可使布局内的控件按照权重比显示大小。在进行屏幕适配时起到关键作用。

适用场景:

  • 简单的线性界面(登录表单:账号 + 密码 + 按钮)
  • 列表项、导航栏、垂直 / 水平排列的控件组
示例代码:
xml 复制代码
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="确认" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="取消" />
</LinearLayout>

2. 相对布局(RelativeLayout)

特点:通过子控件之间的相对位置或与父布局的相对位置来定位。

核心属性:

(1)相对于父布局的属性

  • android:layout_centerInParent:控件居中于父布局
  • android:layout_centerHorizontal:控件水平居中
  • android:layout_centerVertical:控件垂直居中
  • android:layout_alignParentTop:控件顶部与父布局顶部对齐
  • android:layout_alignParentBottom:控件底部与父布局底部对齐
  • android:layout_alignParentLeft:控件左侧与父布局左侧对齐
  • android:layout_alignParentRight:控件右侧与父布局右侧对齐

(2)相对于其他控件的属性

  • android:layout_below:位于指定控件的下方
  • android:layout_above:位于指定控件的上方
  • android:layout_toLeftOf:位于指定控件的左侧
  • android:layout_toRightOf:位于指定控件的右侧
  • android:layout_alignTop:与指定控件的顶部对齐
  • android:layout_alignBottom:与指定控件的底部对齐
  • android:layout_alignLeft:与指定控件的左侧对齐
  • android:layout_alignRight:与指定控件的右侧对齐

适用场景:

  • 控件位置不规则的界面(个人中心、详情页)
  • 需要减少布局嵌套的场景

示例代码:

ini 复制代码
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="标题" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="按钮" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button"
    android:layout_alignLeft="@id/button"
    android:layout_marginTop="10dp"
    android:text="说明文字" />

3. 表格布局(TableLayout)

特点: 以表格(行和列)的形式排列控件,通过标签定义每一行,行内的控件自动成为该行的列。

核心属性:

  • android:shrinkColumns:指定可收缩的列,当内容超出屏幕时,该列会收缩
  • android:stretchColumns:指定可拉伸的列,该列会填满剩余空间
  • android:collapseColumns:指定可隐藏的列

TableRow中的常用属性:

  • android:layout_column:指定控件所在的列索引(从0开始)
  • android:layout_span:指定控件跨多少列

适用场景:

  • 设置页面(如系统设置界面)
  • 表单输入界面
  • 数据表格展示
  • 计算器界面

示例代码:

ini 复制代码
<!-- 第一行:标签 + 输入框 -->
<TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:" />
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="请输入用户名" />
</TableRow>

<!-- 第二行:标签 + 输入框 -->
<TableRow>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:" />
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textPassword"
        android:hint="请输入密码" />
</TableRow>

<!-- 第三行:两个按钮 -->
<TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册" />
</TableRow>

4. 帧布局(FrameLayout)

特点: 最简单的布局方式,所有子控件默认叠加在布局的左上角,后添加的控件会覆盖在先添加的控件之上。

核心属性:

  • android:layout_gravity:控制子控件在布局内的对齐方式。可选值:center、left、right、top、bottom、center_vertical、center_horizontal等,多个值可用|组合(如right|top表示右上角)

适用场景:

  • Fragment容器(如ViewPager的页面容器)
  • 图片加文字遮罩效果
  • 加载动画层叠显示
  • 角标

示例代码:

ini 复制代码
<!-- 背景图片 -->
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/background"
    android:scaleType="centerCrop" />

<!-- 居中显示的遮罩文字 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="文字遮罩"
    android:textColor="#FFFFFF"
    android:textSize="18sp" />

<!-- 右上角角标 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|top"
    android:text="热门"
    android:background="#FF5722"
    android:padding="5dp"
    android:textColor="#FFFFFF"
    android:textSize="12sp" />

5.四种布局对比总结

布局类型 定位方式 优点 缺点 推荐场景
LinearLayout 线性排列 简单易用、支持权重分配 复杂界面嵌套多 登录表单、导航栏
RelativeLayout 相对位置 定位灵活、可减少嵌套 约束复杂时可读性下降 个人中心、详情页
TableLayout 表格排列 结构化好、自动对齐 灵活性较低 设置页面、表单
FrameLayout 图层叠加 最轻量、渲染最快 仅支持简单对齐 Fragment容器、遮罩层
相关推荐
常利兵2 小时前
Android 集合探秘:ArrayMap 与 SparseArray 的奇妙之旅
android·算法·哈希算法
氦客2 小时前
Android Compose 屏幕适配实战:区分手机 / 平板
android·智能手机·电脑
安卓机器2 小时前
安卓玩机工具推荐------电脑端 开源的安卓设备联机玩机辅助工具 强烈推荐
android·电脑
always_TT2 小时前
整数溢出与未定义行为
android
Digitally3 小时前
6 种实用方法:无需 USB 线将电脑文件传输至安卓手机
android·智能手机·电脑
秋93 小时前
Pentaho Kettle 9.4 实战:SQL Server 数据同步到 MySQL详细手册,附详细手册
android·adb·数据库同步
PHP代码3 小时前
windows mysql 双板本 兼容性
android·adb
深念Y3 小时前
魅蓝Note5 Root + 改内核激活命名空间:让Docker跑在安卓上
android·linux·服务器·docker·容器·root·服务
向上_503582914 小时前
两个moudle访问一个lib包
android·java·kotlin