Android 资源类型全列举 + 布局资源4种常用类型详解
Android 资源(Resources)是独立于代码的静态内容(图片、文字、布局、样式等),遵循「代码与资源分离」设计,方便多设备适配、国际化和维护。
一、Android 全资源类型完整列举
Android 资源统一放在 res/ 目录下,按功能分为10大类核心资源,每类对应专属子目录:
| 资源目录 | 资源类型 | 核心作用 |
|---|---|---|
res/layout/ | 布局资源 | 定义界面的控件排列、结构(最常用核心资源) |
res/drawable/ | 图片/图形资源 | 位图(png/jpg)、矢量图、形状、选择器 |
res/mipmap/ | 应用图标资源 | 不同分辨率的桌面/启动器图标 |
res/values/ | 值资源 | 文字、颜色、尺寸、样式、数组、布尔值、整数 |
res/menu/ | 菜单资源 | 选项菜单、上下文菜单、底部导航菜单 |
res/anim/ | 补间动画资源 | 视图的平移、旋转、缩放、淡入淡出动画 |
res/animator/ | 属性动画资源 | 更强大的动画(支持控件属性动态修改) |
res/raw/ | 原生文件资源 | 音频、视频、HTML等不编译的二进制文件 |
res/xml/ | 自定义XML资源 | 配置文件、数据结构、自定义解析文件 |
res/font/ | 字体资源 | 自定义TTF/OTF字体文件 |
二、布局资源(Layout)4种常用类型详解
布局资源是定义Android界面结构的XML文件,核心作用是管理控件的位置、大小、层级和适配规则。
Android 提供4种最常用、最核心的布局,适配不同界面场景:
1. LinearLayout(线性布局)
核心特性
- 最基础、最常用的布局,控件按单一方向排列:水平/垂直
- 控件依次排队,不会重叠,自带自动换行/自动撑满特性
- 支持权重(
layout_weight)实现比例分配空间(适配神器)
关键属性
-
android:orientation:排列方向vertical:垂直(从上到下)horizontal:水平(从左到右)
-
android:layout_weight:权重,按比例分配剩余空间 -
android:gravity:内部控件对齐方式(居中、居左、居右等)
适用场景
- 简单的线性界面(登录表单、列表项、按钮栏、输入框组)
- 需要按比例划分宽度/高度的界面
示例代码
<?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:orientation="vertical" <!-- 垂直排列 -->
android:gravity="center_horizontal">
<!-- 权重1:占1/3高度 -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="按钮1"/>
<!-- 权重2:占2/3高度 -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text="按钮2"/>
</LinearLayout>
2. RelativeLayout(相对布局)
核心特性
- 控件以其他控件/父布局为参照物定位,自由度极高
- 支持:居中、居左、居右、上方、下方、对齐、重叠等任意位置
- 无需嵌套即可实现复杂界面,减少布局层级(性能更优)
关键属性
-
相对于父布局:
android:layout_centerInParent:居中android:layout_alignParentRight:靠右android:layout_alignParentBottom:靠下
-
相对于其他控件(需指定控件id):
android:layout_below:在某控件下方android:layout_toRightOf:在某控件右侧android:layout_alignTop:与某控件顶部对齐
适用场景
- 不规则界面、控件需要精准定位、重叠/对齐需求
- 头像+昵称+描述的组合布局、自定义item
示例代码
<?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">
<!-- 父布局居中 -->
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="居中文字"
android:layout_centerInParent="true"/>
<!-- 在居中文字下方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下方按钮"
android:layout_below="@id/tv_center"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
3. ConstraintLayout(约束布局)
核心特性
- Android官方推荐的首选布局(替代LinearLayout+RelativeLayout)
- 完全扁平化:无嵌套实现任意复杂界面,性能最优
- 支持:约束链、百分比、辅助线、屏障、组控制
- Android Studio可视化编辑器完美支持(拖拽即可生成布局)
关键属性
所有控件必须设置至少一对水平+垂直约束:
- 水平约束:
layout_constraintLeft_toLeftOf/toRightOf - 垂直约束:
layout_constraintTop_toTopOf/toBottomOf - 居中:
layout_constraintStart_toStartOf+layout_constraintEnd_toEndOf - 百分比宽高:
layout_constraintWidth_percent
适用场景
- 所有复杂界面、列表item、适配各种屏幕
- 企业级项目默认首选布局
示例代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 居中按钮 -->
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="约束布局按钮"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4. FrameLayout(帧布局)
核心特性
- 最简单的布局,所有控件默认叠加在左上角
- 后写的控件会覆盖先写的控件(层级关系)
- 无复杂定位,仅支持简单对齐(gravity)
- 轻量、渲染速度极快
关键属性
android:gravity:控件对齐方式(居中、居右、居底等)
适用场景
- 图层叠加(图片+文字、Fragment容器、加载动画遮罩)
- 单控件展示、标签角标、悬浮按钮
示例代码
<?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="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
<!-- 上层居中文字(覆盖图片) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帧布局上层文字"
android:textSize="20sp"
android:textColor="#fff"
android:layout_gravity="center"/>
</FrameLayout>
三、4种常用布局快速对比(记忆版)
| 布局名称 | 定位方式 | 优点 | 缺点 | 推荐场景 |
|---|---|---|---|---|
| LinearLayout | 线性排列 | 简单易用、支持权重 | 复杂界面需嵌套 | 简单线性界面 |
| RelativeLayout | 相对定位 | 自由度高、少嵌套 | 复杂布局性能一般 | 中等复杂度界面 |
| ConstraintLayout | 约束定位 | 扁平化、性能最优、官方推荐 | 学习成本略高 | 所有界面首选 |
| FrameLayout | 叠加层 | 最轻量、速度最快 | 仅支持简单对齐 | 图层叠加、容器 |
总结
- Android 核心资源共10类,布局、图片、值资源是开发中最常用;
- 布局资源是界面核心,ConstraintLayout 是现代开发首选;
- 简单界面用 LinearLayout/FrameLayout,复杂界面用 ConstraintLayout。