android资源类型

7 阅读5分钟

在 Android 开发中,资源(Resources)是应用中独立于代码的静态内容,通过res/目录组织。下面先列举所有标准资源类型,然后详细介绍四种常用布局资源。

一、Android 资源类型总览****

资源目录****存放内容****资源类型(R 类中的子类)****
anim/属性动画(ObjectAnimator 等)XMLR.anim
animator/补间动画(透明度、旋转等)XMLR.animator
color/颜色状态列表(ColorStateList),如按钮不同状态的颜色R.color
drawable/位图、矢量图、形状(Shape)、九宫格、图层列表等R.drawable
layout/布局 XML 文件R.layout
menu/菜单资源(选项菜单、上下文菜单等)R.menu
mipmap/应用图标(系统会针对不同密度优化,推荐在此存放)R.mipmap
values/简单值:字符串、尺寸、颜色、数组、样式、主题等R.string,R.dimen,R.color,R.array,R.style,R.bool,R.integer
xml/任意 XML 文件,如首选项配置、搜索配置等R.xml
raw/任意原始文件(如音频、视频等),以原文件名访问R.raw
font/字体文件(.ttf / .otf)或字体家族 XMLR.font

另外还有res/values-xx/、layout-land/等限定符目录用于适配不同配置(语言、屏幕方向、屏幕尺寸等)。

二、布局资源详细介绍****

布局资源是 Android UI 的基础,定义了界面中视图(View)和视图组(ViewGroup)的层次结构。四种最常用的布局类型分别是:

1. LinearLayout(线性布局)

2. RelativeLayout(相对布局)

3. ConstraintLayout(约束布局)

4. FrameLayout(帧布局)

1. LinearLayout(线性布局)****

特点:将子视图按单一方向(水平或垂直)顺序排列,支持权重(weight)分配剩余空间。

核心属性

· android:orientation:horizontal(水平)或vertical(垂直)

· android:layout_weight:分配剩余空间的比例,需结合android:layout_width或android:layout_height为0dp(在对应方向上)使用

· android:gravity:控制子视图在容器内的对齐方式

· android:layout_gravity:控制子视图在父容器中的对齐方式

适用场景:简单的列表式布局、表单、工具栏等规则排列的场景。

示例

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center_vertical">

<TextView

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Name" />

<EditText

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:hint="Enter name" />

优缺点

· ✅ 简单直观,适合线性排列

· ❌ 嵌套过多会导致性能下降(measure 次数增加)

2. RelativeLayout(相对布局)****

特点:子视图之间通过相对位置(如layout_toRightOf、layout_alignParentTop)进行定位,灵活性较高。

核心属性

· 相对于父容器:layout_alignParentTop、layout_centerInParent等

· 相对于其他视图:layout_toRightOf、layout_below、layout_alignLeft等

适用场景:复杂但非嵌套的平面布局,如表单的标签与输入框对齐、简单的自适应界面。

示例

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/label"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:text="Header" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/label"

android:layout_centerHorizontal="true"

android:text="Submit" />

优缺点

· ✅ 避免多层嵌套,单一平面内可以完成复杂相对关系

· ❌ 约束关系不够直观,维护时易出错;性能上不如 ConstraintLayout(尤其在复杂布局中)

3. ConstraintLayout(约束布局)****

特点:通过约束(constraints)定义视图之间的相对位置,支持扁平化布局,极大减少嵌套层级,是 Android 官方推荐的现代布局方式。

核心属性

· 位置约束:layout_constraintStart_toStartOf、layout_constraintTop_toBottomOf等

· 尺寸控制:layout_constraintWidth_percent(百分比宽度)、match_constraint(0dp 表示遵循约束)

· 偏移:layout_constraintHorizontal_bias(偏移比例)

· 链(Chains):一组视图在水平或垂直方向上的双向约束,可均匀分布

适用场景:几乎任何界面,尤其适合复杂但需高性能的布局。

示例

<androidx.constraintlayout.widget.ConstraintLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/title"

android:layout_width="0dp"

android:layout_height="wrap_content"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintTop_toTopOf="parent"

android:text="Title" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:layout_constraintTop_toBottomOf="@id/title"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:text="Click" />

</androidx.constraintlayout.widget.ConstraintLayout>

优缺点

· ✅ 扁平化,性能好(测量和布局阶段高效)

· ✅ 强大的布局能力(百分比、链、圆形定位等)

· ✅ Android Studio 提供可视化编辑器支持拖拽

· ❌ 学习曲线稍陡,XML 较冗长

4. FrameLayout(帧布局)****

特点:最简单的布局,所有子视图默认堆叠在左上角(可通过layout_gravity改变位置),后添加的视图会覆盖在先添加的之上。

核心属性

· android:foreground:可以在所有子视图上方绘制一个前景图层

· android:foregroundGravity:前景的位置

适用场景

· 作为容器存放单个视图(如 Fragment 容器)

· 实现叠加效果(如进度条浮在内容上方)

· 搭配visibility实现视图切换

示例

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:src="@drawable/background" />

<ProgressBar

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center" />

优缺点

· ✅ 极简,性能开销小

· ❌ 不适合复杂布局,只能简单叠加或单一子视图

三、布局选择建议****

场景****推荐布局****
简单线性排列LinearLayout
单子视图或叠加层FrameLayout
复杂关系,避免嵌套ConstraintLayout
早期项目或简单相对关系RelativeLayout(逐渐被 ConstraintLayout 取代)

在现代 Android 开发中,ConstraintLayout是最通用且性能良好的选择,尤其配合 Android Studio 的布局编辑器,可以高效构建大多数界面。其他布局则在特定场景下依然有使用价值。