Android 资源类型全列举 + 布局资源 4 种常用类型详解

7 阅读6分钟

Android 资源(Resources)是独立于代码的静态内容(图片、文字、布局、样式等),遵循「代码与资源分离」设计,方便多设备适配、国际化、主题切换。所有资源统一存放在 res/ 目录下,系统会自动在 R.java 中生成唯一 ID 供代码调用。

一、Android 全资源类型完整列举

res/ 子目录分类,包含所有标准资源类型

资源目录资源类型核心作用
res/layout/布局资源定义界面结构(线性、相对、约束布局等)
res/drawable/图片 / 矢量资源位图(png/jpg)、矢量图(xml)、形状、选择器
res/mipmap/应用图标资源不同分辨率启动图标(替代旧版 drawable 存放图标)
res/values/值资源文字、颜色、尺寸、样式、数组、布尔值、整数
res/anim/补间动画资源视图渐变、缩放、平移、旋转动画(xml 定义)
res/animator/属性动画资源更强大的对象属性动画(Android 3.0+)
res/color/颜色状态资源按钮按下 / 选中 / 禁用的颜色变化(xml)
res/font/字体资源自定义 TTF/OTF 字体
res/raw/原生资源音频、视频、二进制文件(不编译,直接读取)
res/xml/自定义 XML 资源配置文件、数据结构(非布局 / 动画)
res/menu/菜单资源选项菜单、上下文菜单、弹出菜单
res/interpolator/插值器资源定义动画速度变化规律(先快后慢等)

二、布局资源(Layout)4 种最常用类型详解

布局资源是Android 界面的骨架,负责管理控件(TextView/Button 等)的位置、大小、层级关系,文件后缀为 .xml,存放在 res/layout/ 下。

Android 开发中最核心、最常用的 4 种布局:

  1. LinearLayout(线性布局)
  2. RelativeLayout(相对布局)
  3. ConstraintLayout(约束布局)
  4. FrameLayout(帧布局)

1. LinearLayout(线性布局)

核心特性

  • 最基础、最简单的布局,控件按「水平 / 垂直」方向线性排列
  • 核心属性:android:orientation 控制排列方向
  • 支持权重(layout_weight)实现按比例分配空间(适配神器)

关键属性

属性作用
android:orientation="horizontal"水平排列(从左到右)
android:orientation="vertical"垂直排列(从上到下)
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高度 -->
    <TextView
        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="按钮"/>

</LinearLayout>

适用场景

  • 简单的线性界面(登录表单、列表项、垂直 / 水平排列的控件组)
  • 需要等比例 / 按权重分配空间的场景

2. RelativeLayout(相对布局)

核心特性

  • 以「控件之间的相对位置」或「父容器对齐」定位
  • 无需嵌套,一个布局就能完成复杂排版,减少布局层级
  • Android 早期主流布局,现在逐步被约束布局替代

关键属性

  1. 相对于父容器对齐layout_alignParentTop(顶部对齐)、layout_centerInParent(居中)
  2. 相对于其他控件对齐layout_below(在某控件下方)、layout_toRightOf(在某控件右侧)
  3. 基准对齐layout_alignLeft(与某控件左对齐)

代码示例

<?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:layout_centerInParent="true"
        android:text="居中文字"/>

    <!-- 在tv_center上方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv_center"
        android:text="上方按钮"/>

</RelativeLayout>

适用场景

  • 控件之间有相对位置依赖的界面
  • 不想多层嵌套的简单复杂布局(现已不推荐作为首选)

3. ConstraintLayout(约束布局)【官方推荐首选】

核心特性

  • Google 官方默认、最强、最推荐的布局(Android Studio 新建项目默认)
  • 扁平化设计:无嵌套即可实现任意复杂界面,大幅提升渲染性能
  • 支持百分比布局、链布局、辅助线、屏障等高级功能
  • 可视化编辑友好(拖拽控件即可自动生成约束)

核心约束规则

控件必须至少定义水平 + 垂直各一条约束,否则会报错:

  1. layout_constraintLeft_toLeftOf:左边缘对齐目标左边缘
  2. layout_constraintTop_toTopOf:上边缘对齐目标上边缘
  3. layout_constraintStart_toEndOf:起始边在目标结束边右侧
  4. layout_constraintHorizontal_bias:水平偏移百分比(0-1)

代码示例

<?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">

    <!-- 居中:上下左右都约束到父容器 -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="约束布局居中"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!-- 在tv下方,水平居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        app:layout_constraintTop_toBottomOf="@id/tv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

适用场景

  • 所有界面(尤其是复杂 UI、适配平板 / 折叠屏)
  • 追求性能、减少布局嵌套的项目

4. FrameLayout(帧布局)

核心特性

  • 最简单的布局:所有控件默认叠加在左上角
  • 后写的控件会覆盖先写的控件(层级关系)
  • 无复杂定位,仅支持简单对齐(居中、居底等)

关键属性

android:gravity:控制子控件对齐(center、bottom、right 等)

代码示例

<?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"
    android:gravity="center">

    <!-- 底层图片 -->
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher"/>

    <!-- 上层文字,覆盖在图片上 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="叠加文字"
        android:textSize="20sp"/>

</FrameLayout>

适用场景

  • 图层叠加(图片 + 角标、视频播放层、加载动画遮罩)
  • Fragment 容器、ViewPager 页面容器
  • 简单的单控件居中展示

三、4 种常用布局核心对比总结

布局类型定位方式嵌套层级性能推荐度核心场景
LinearLayout线性(水平 / 垂直)易嵌套一般低 - 中简单线性排列、权重比例
RelativeLayout相对位置低嵌套良好简单相对定位(旧项目)
ConstraintLayout约束定位0 嵌套极高高(最推荐)所有界面、复杂 UI、高性能
FrameLayout叠加对齐无嵌套最高图层叠加、Fragment 容器

总结

  1. Android 资源共分 13 大类,核心是布局、图片、值、动画、菜单五大类;
  2. 布局资源是界面骨架,ConstraintLayout 是现代开发唯一首选
  3. 简单线性排列用 LinearLayout,叠加效果用 FrameLayout,相对定位仅用于旧项目。