把android资源类型详解

一、Android 主要资源类型

类型 目录
动画 anim/
属性动画 animator/
颜色 color/、values/colors.xml
图片/形状 drawable/
布局 layout/
菜单 menu/
字符串 values/strings.xml
样式/主题 values/styles.xml
尺寸 values/dimens.xml
数组 values/arrays.xml
字体 font/
原始文件 raw/
任意XML xml/

二、四种常用布局资源

布局 特点 适用场景
LinearLayout 水平/垂直线性排列 简单列表、表单
RelativeLayout 相对定位(相对于父容器或其他控件) 中等复杂度的相对布局
FrameLayout 堆叠,默认左上角 Fragment容器、叠加效果
ConstraintLayout 约束定位,扁平化,性能好 复杂布局,替代多层嵌套

1、LinearLayout(线性布局)

核心特点

  • 子视图按水平 或垂直方向线性排列
  • 支持权重(weight) 分配剩余空间

关键属性

属性 说明
orientation horizontal(水平)/ vertical(垂直)
layout_weight 权重值,按比例分配剩余空间
gravity 子视图在布局中的对齐方式
layout_gravity 自身在父布局中的对齐方式

示例

xml

ini 复制代码
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标题" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="按钮" />

</LinearLayout>

适用场景 :简单列表、表单页面
注意事项:嵌套过深会影响性能,建议控制层级


2. RelativeLayout(相对布局)

核心特点:通过相对关系定位(相对于父容器或其他控件),布局扁平。

关键属性:

  • 相对于父容器 :layout_alignParentTop、layout_centerInParent 等
  • 相对于其他控件 :layout_above、layout_below、layout_toLeftOf 等
  • 对齐属性 :layout_alignTop、layout_alignBottom 等

示例代码:

xml

ini 复制代码
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="标题" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:text="确定" />

</RelativeLayout>

适用场景 :中等复杂度的相对定位布局
注意事项 :需要为参照控件设置 id


3、FrameLayout(帧布局)

核心特点

  • 子视图默认堆叠在左上角
  • 后添加的覆盖先添加的
  • 结构简单,通常只放一个子视图

关键属性

属性 说明
foreground 前景图片,始终在最上层
foregroundGravity 前景图片对齐方式
layout_gravity 子视图在容器中的位置

示例

xml

ini 复制代码
<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/bg" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="加载中" />

</FrameLayout>

适用场景

  • Fragment 容器
  • 叠加层(进度条、遮罩)
  • 单个子视图的简单布局

4、ConstraintLayout(约束布局)

核心特点

  • 通过约束定义位置关系
  • 扁平化设计,避免多层嵌套
  • Google 官方推荐的主流布局

关键属性

属性 说明
layout_constraintLeft_toLeftOf 左边与目标左边对齐
layout_constraintLeft_toRightOf 左边位于目标右边
layout_constraintTop_toBottomOf 顶部位于目标底部
layout_constraintBottom_toTopOf 底部位于目标顶部
layout_constraintStart_toEndOf 推荐使用,支持 RTL
layout_constraintHorizontal_bias 水平偏向(0~1)
layout_constraintWidth_percent 宽度百分比

示例

xml

ini 复制代码
<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"
        android:text="标题"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

适用场景 :复杂布局、替代多层嵌套
注意事项 :需要添加依赖 androidx.constraintlayout:constraintlayout

相关推荐
tongyiixiaohuang2 小时前
WMS已发货状态反写金蝶云星空:单策略实战教程
android
好的佩奇3 小时前
Dart 之类修饰符
android·flutter·dart
alexhilton5 小时前
用Konsist与Lint强制执行设计系统契约
android·kotlin·android jetpack
北京自在科技8 小时前
谷歌安卓 Gemini 迎来界面改版,Find Hub 接入 Gemini 支持物品记忆查找
android·安卓
YB137511 小时前
Jetpack Compose 动画Modifier.animateItem()
android
2401_8858850411 小时前
巴林语音通知接口如何对接?系统告警语音API接入教程
android·前端·网络·python·语音识别
收纳过往12 小时前
数据库加索引的时机:慢查询日志说话
android·开发语言·数据库·python·低代码·rxjava
狂热开发者13 小时前
车载中控要给鸿蒙用户,能不能和 Android 共用一个二维码?
android·华为·harmonyos
.Bryce.14 小时前
T2-LLM评测不等于Agent评测
android·java·人工智能·后端·gpt·agent·1024程序员节
美狐美颜SDK开放平台15 小时前
视频美颜SDK如何应对快速转头与大幅度面部动作?
android·人工智能·ios·音视频·直播美颜sdk·视频美颜sdk