Android 资源类型全解析及四大常用布局资源深度指南

8 阅读8分钟

在 Android 开发中,资源是应用不可或缺的组成部分,它们允许我们将代码与界面、字符串、图像等内容分离,提高应用的可维护性和适配性。本文将全面列举 Android 资源类型,并详细介绍其中布局资源的四种常用类型,帮助你在开发中更高效地使用它们。

一、Android 资源类型总览

Android 资源通常存放在 res/ 目录下,不同类型的资源对应不同的子目录。以下是常见的资源类型及其目录说明:

资源分类存放目录核心说明
布局资源res/layout/定义应用 UI 界面结构的 XML 文件,是开发中最高频的资源之一,四大布局均在此目录下编写
字符串资源res/values/strings.xml存放文本字符串,支持多语言国际化(如 values-zh/strings.xml),是硬编码文本的最佳替代方案
颜色资源res/values/colors.xml统一管理色值(支持 HEX、ARGB 格式),可实现全局主题一键切换
尺寸资源res/values/dimens.xml定义控件宽高、间距、字体大小等尺寸(推荐用 dp/sp),适配不同屏幕密度
图片与 Drawable 资源res/drawable-xxxdpi/存放 PNG/JPG/WebP 图片,以及 XML 定义的 Shape(形状)、Selector(选择器)、LayerList(层叠)等自定义 Drawable
Mipmap 资源res/mipmap-xxxdpi/专门存放应用启动图标,系统会根据设备屏幕密度自动缩放优化,性能优于 drawable
主题和样式资源res/values/styles.xml/themes.xml定义 View/Activity/Application 的复用样式(Style)与全局主题(Theme),统一界面风格
菜单资源res/menu/定义选项菜单、上下文菜单、底部导航菜单等 XML 文件
补间动画资源res/anim/定义平移、缩放、旋转、透明度四大补间动画的 XML 文件
属性动画资源res/animator/定义属性动画的 XML 文件,支持对任意对象的属性做动画控制
原始资源res/raw/存放音视频、文本、数据库等原始文件,编译时不处理直接打包,通过 R.raw.xxx 访问
数组资源res/values/arrays.xml定义字符串数组、整型数组,用于列表、下拉框等场景
自定义 XML 资源res/xml/存放自定义配置 XML,如 SharedPreferences 配置、设备管理配置等
字体资源res/font/存放自定义字体文件(TTF/OTF 格式),Android 8.0 及以上原生支持

二、布局资源详解

布局资源是 Android 开发中最常用的资源类型之一,用于定义应用的界面结构。以下是四种最常用的布局类型,我们将从特点、常用属性、示例代码和优缺点等方面进行介绍。

1. LinearLayout(线性布局)

特点

水平或垂直方向依次排列子控件,方向通过 orientation 属性控制。

子控件按顺序排列,不会重叠,适合简单的线性界面结构。

常用属性
属性说明
android:orientation布局方向,可选值:horizontal(水平)、vertical(垂直,默认)。
android:layout_weight子控件的权重,用于按比例分配剩余空间(需配合 layout_widthlayout_height0dp 使用)。
android:gravity布局内子控件的对齐方式(如 centerleftright 等)。
示例代码
<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">
    
    <!-- 水平排列的子布局 -->
<LinearLayout 
    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="按钮1" />
    
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:text="按钮2" />
    </LinearLayout>
    
    <!-- 垂直排列的文本 --> 
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello LinearLayout!"
        android:layout_marginTop="20dp" />
</LinearLayout>
优缺点

优点:简单易用,适合线性排列的界面,权重属性可灵活分配空间。

缺点:复杂界面可能需要嵌套多层,影响性能;无法实现子控件的相对定位。

2. RelativeLayout(相对布局)

特点

子控件通过相对关系定位(如相对于父布局或其他子控件的位置),灵活性更高。

可减少布局嵌套层级,提升性能,适合复杂的界面结构。

属性说明
android:layout_alignParentTop子控件顶部与父布局顶部对齐。
android:layout_centerInParent子控件在父布局中居中(水平 + 垂直)。
android:layout_toRightOf子控件位于指定控件的右侧(需引用控件 ID)。
android:layout_below子控件位于指定控件的下方(需引用控件 ID)。
示例代码
<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_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="相对布局示例" 
        android:textSize="20sp" 
        android:layout_centerInParent="true" />

    <!-- 标题下方的按钮 --> 
    <Button 
        android:id="@+id/btn_click"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="点击我"
        android:layout_below="@id/tv_title"
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="20dp" /> 
    
    <!-- 按钮右侧的文本 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右侧说明"
        android:layout_toRightOf="@id/btn_click" 
        android:layout_alignBottom="@id/btn_click" 
        android:layout_marginLeft="10dp" /> 
</RelativeLayout>
优缺点

优点:灵活性强,可通过相对关系实现复杂布局,减少嵌套层级。

缺点:属性较多,相对关系复杂时维护成本较高;性能略低于 ConstraintLayout。

3. FrameLayout(帧布局)

特点

最简单的布局类型,所有子控件默认堆叠在布局的左上角,后添加的子控件会覆盖之前的。

适合展示单一内容或需要层叠效果的界面(如 Fragment 容器、悬浮按钮)。

常用属性
属性说明
android:foreground设置布局的前景图像(可覆盖在所有子控件之上)。
android:foregroundGravity前景图像的对齐方式。
android:layout_gravity子控件在布局中的对齐方式(如 centerbottom 等)。
示例代码
<FrameLayoutxmlns: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" 
        android:scaleType="centerCrop" />

    <!-- 中层半透明遮罩 -->
    <View 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#80000000" />

    <!-- 顶层居中的文本 -->
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="帧布局示例" 
        android:textColor="#FFFFFF"
        android:textSize="24sp" 
        android:layout_gravity="center" />
 </FrameLayout>
优缺点

优点:简单轻量,适合层叠效果或单一内容展示。

缺点:子控件默认堆叠,无法实现复杂的排列方式,适用场景有限。

4.TableLayout(表格布局)

特点

表格布局是 LinearLayout 的子类,专门用于实现表格形式的行列排列,结构类似 HTML 的 <table>。它通过 TableRow 定义行,每个 TableRow 中的子控件对应一个单元格。

核心规则

行数由 TableRow 数量决定,列数由列数最多的 TableRow 决定

默认无分割线,需自定义实现

常用属性
属性名作用说明
android:stretchColumns设置可拉伸的列(列序号从 0 开始),内容不足时自动拉伸填满空间,* 代表所有列
android:shrinkColumns设置可收缩的列,内容过多时自动收缩换行,避免溢出
android:layout_span子控件跨越的列数(类似 HTML 的 colspan),默认 1 列
示例代码
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="*"> 
    
    <!-- 表头行 --> 
    <TableRow> 
        <TextView 
            android:text="序号" 
            android:gravity="center"
            android:textStyle="bold"/> 
        <TextView 
            android:text="商品名称" 
            android:gravity="center" 
            android:textStyle="bold"/>
        <TextView 
            android:text="单价" 
            android:gravity="center" 
            android:textStyle="bold"/>
    </TableRow> 
    
    <!-- 数据行 --> 
    <TableRow> 
        <TextView
            android:text="1"
            android:gravity="center"/>
        <TextView
            android:text="Android开发艺术探索" 
            android:gravity="center"/> 
        <TextView 
            android:text="¥79" 
            android:gravity="center"/> 
     </TableRow>
     
     <!-- 合计行(跨2列) --> 
     <TableRow>
         <TextView 
             android:text="合计" 
             android:layout_span="2" 
             android:gravity="right"
             android:textStyle="bold"/>
          <TextView
          android:text="¥158"
          android:gravity="center"
          android:textColor="#FF5722"/>
      </TableRow> 
      
   </TableLayout>
 
优缺点

优点:非常适合表格类、行列对齐的界面,实现简单,逻辑清晰。

缺点:灵活性有限,仅适合表格场景;无法实现单元格跨行(仅支持跨列)。

三、四大布局适用场景对比

布局类型核心优势核心劣势首选适用场景
LinearLayout简单直观、权重适配方便复杂界面嵌套多、性能差线性排列的简单界面、表单、导航栏
RelativeLayout灵活性强、布局层级少属性繁多、维护成本高非规则排列的复杂界面、个人中心
TableLayout行列对齐逻辑清晰灵活性差、仅适合表格数据报表、订单列表、计算器
FrameLayout最轻量、适合层叠功能单一Fragment 容器、闪屏页、悬浮控件

在实际开发中,我们无需局限于单一布局,可根据界面需求灵活组合,在实现效果的同时兼顾性能与可维护性。

总结

Android 资源体系是实现代码与 UI 分离、提升应用可维护性和多设备适配性的核心。本文梳理的 14 类资源中,布局资源无疑是开发中最高频的部分,而 LinearLayout、RelativeLayout、TableLayout、FrameLayout 这四大经典布局,更是构建 Android UI 的基石。