Android 资源类型全解 + 布局资源详解

2 阅读5分钟

一、Android 资源类型全列举

Android 资源是存放在 res/ 目录下的非代码文件(图片、布局、字符串、样式等),系统会自动生成对应 R 类索引,核心分类如下:

表格

资源目录资源类型作用
res/drawable/图片 / 矢量资源存放位图(png/jpg)、矢量图(xml)、形状资源、状态选择器
res/layout/布局资源定义界面结构、控件排列方式
res/values/值资源字符串(strings.xml)、颜色(colors.xml)、尺寸(dimens.xml)、样式(styles.xml)、数组(arrays.xml)、主题(themes.xml)
res/mipmap/启动图标存放不同分辨率的 APP 启动图标(替代 drawable 存放图标)
res/anim/补间动画资源定义控件平移、缩放、旋转、淡入淡出动画
res/animator/属性动画资源更强大的动画(支持控件属性动态修改)
res/raw/原生文件存放音频、视频、文本等原始文件(不编译)
res/xml/自定义 XML 资源配置文件、数据解析文件
res/font/字体资源存放自定义 TTF/OTF 字体文件
res/menu/菜单资源定义选项菜单、上下文菜单、弹出菜单
res/color/颜色状态资源定义控件不同状态(按下 / 选中)的颜色

二、布局资源 4 种常用类型详解

布局资源(res/layout/*.xml)是 Android 界面的核心,用于排列界面控件(按钮、文本、图片等) ,4 种最常用布局:

1. LinearLayout(线性布局)

核心特点

  • 最基础、最常用的布局,按水平 / 垂直方向线性排列控件
  • 控件依次排布,不会重叠
  • 通过 orientation 属性指定方向,layout_weight 实现权重比例分配

关键属性

  1. android:orientation:排列方向

    • vertical:垂直(从上到下)
    • horizontal:水平(从左到右)
  2. android:layout_weight:权重(按比例分配剩余空间)

  3. android:gravity:内部控件对齐方式

  4. android:layout_gravity:自身在父布局中的对齐方式

代码示例

xml

<?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"> <!-- 内部控件水平居中 -->

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_marginTop="20dp"/> <!-- 顶部间距 -->

</LinearLayout>

适用场景

  • 简单的单列 / 单行界面
  • 表单、列表项、导航栏等线性排列场景

2. RelativeLayout(相对布局)

核心特点

  • 以其他控件 / 父布局为参照定位,灵活性极高
  • 控件可上下左右对齐、居中、重叠摆放
  • 无需嵌套即可实现复杂界面,减少布局层级

关键属性

  1. 相对于父布局:

    • android:layout_centerInParent:居中
    • android:layout_alignParentTop:顶部对齐
    • android:layout_alignParentEnd:右侧对齐
  2. 相对于其他控件:

    • android:layout_below:在某控件下方
    • android:layout_toEndOf:在某控件右侧
    • android:layout_alignTop:与某控件顶部对齐

代码示例

xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 相对布局:按钮2 依附 按钮1 定位 -->
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 居中的按钮1 -->
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_centerInParent="true"/>

    <!-- 按钮2 在按钮1下方,水平居中 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_below="@id/btn1"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

适用场景

  • 控件位置相互依赖的复杂界面
  • 登录页、个人中心、不规则排列的界面

3. ConstraintLayout(约束布局)

核心特点

  • Android 官方推荐首选布局,功能最强大
  • 完全通过约束条件定位(上下左右、比例、居中)
  • 扁平化布局,无嵌套即可实现任意界面,性能最优
  • 支持可视化编辑(Android Studio 直接拖拽)

关键属性

  1. 基础约束:

    • app:layout_constraintTop_toTopOf:顶部对齐
    • app:layout_constraintStart_toStartOf:左侧对齐
    • app:layout_constraintEnd_toEndOf:右侧对齐
  2. 高级功能:

    • 宽高比 layout_constraintDimensionRatio
    • 链布局(链式排列控件)
    • 百分比布局

代码示例

xml

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

适用场景

  • 所有商业项目、复杂界面
  • 替代 LinearLayout + RelativeLayout 嵌套
  • 适配平板、折叠屏、多分辨率设备

4. FrameLayout(帧布局)

核心特点

  • 最简单的布局,所有控件默认叠加在左上角
  • 后添加的控件会覆盖在先添加的控件之上
  • 层级清晰,性能极高

关键属性

  • android:gravity:控制控件整体对齐(居中、底部等)
  • 无复杂定位属性,仅支持简单对齐

代码示例

xml

<?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="200dp">

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

    <!-- 上层文字,居中显示 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="帧布局叠加文字"
        android:textColor="#fff"
        android:textSize="20sp"
        android:layout_gravity="center"/> <!-- 居中叠加 -->

</FrameLayout>

适用场景

  • 图层叠加(图片 + 角标、视频 + 控制栏)
  • Fragment 容器、ViewPager 页面
  • 加载动画、弹窗背景

三、4 种布局核心对比

表格

布局排列方式复杂度性能推荐场景
LinearLayout线性(水平 / 垂直)简单线性界面
RelativeLayout相对定位控件相互依赖
ConstraintLayout约束定位极高所有复杂界面(首选)
FrameLayout叠加层极低极高图层叠加、容器

总结

  1. Android 资源分 10+ 大类,布局资源(layout) 是界面开发核心;
  2. 4 种常用布局:ConstraintLayout 首选,LinearLayout 简单场景,RelativeLayout 灵活定位,FrameLayout 叠加场景。