Android 学习记录

_

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"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/main"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity">  
  
    <TextView        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="姓名:xxxx"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
    <TextView        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="专业:电子信息工程"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
    <TextView        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="学号:xxxxxxx"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
  
</androidx.constraintlayout.widget.ConstraintLayout>

问题是会变成依托狗屎在一起,非常的不美观,那要进行改造,适合的方法是:

方案一:改成 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名:xxx" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="专业:电子信息工程" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学号:xxx" />

</LinearLayout>
  • android:orientation="vertical":三个 TextView 从上到下排列。

  • android:gravity="center":整体在屏幕中居中(想要垂直居中+水平居中)。

  • 如果只想水平居中、垂直靠上,把 gravity 改成 center_horizontal 即可。

方案二:保留 ConstraintLayout,用约束实现垂直排列

在 ConstraintLayout 里做垂直排列:第一个元素用 app:layout_constraintTop_toTopOf="parent" 贴顶,后面每个元素都用 app:layout_constraintTop_toBottomOf="@id/上一个元素的ID" 接在上一个下面,同时每个元素都加上 app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent" 实现水平居中,再用 android:layout_marginTop 控制间距即可。

一、核心规律(最重要)

下一个的 Top,接上一个的 Bottom

元素 关键约束
第 1 个 app:layout_constraintTop_toTopOf="parent"
第 2 个 app:layout_constraintTop_toBottomOf="@id/上一个的ID"
第 3 个 app:layout_constraintTop_toBottomOf="@id/上一个的ID"
以此类推

二、水平居中(每个元素都要写)

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

StartEnd 成对出现,缺一不可,否则居中失效。


三、控制行间距

android:layout_marginTop="16dp"
  • marginTop 控制当前元素与上方元素的距离。
  • 第 1 个用较大的值(如 32dp)拉开与顶部的距离。

四、常见错误对照表

❌ 错误写法 ✅ 正确写法 问题
layout_constraintBottom_toBottomOf="@id/上一个" layout_constraintTop_toBottomOf="@id/上一个" 方向写反 → 元素重叠
只写 Bottom_toBottomOf,没有 Top 约束 必须有明确的 Top 约束 约束不完整 → 位置乱跑
少写 StartEnd 两个都写 无法水平居中

五、ID 命名规范

  • 完整正确的英文单词,如 tvMajortvId
  • 避免拼写错误(如 tvMaiortvTd),否则后期引用容易出错
  • 前缀建议:tv(TextView)、btn(Button)、et(EditText)

六、ConstraintLayout vs LinearLayout 选择

场景 推荐
简单一列/一行排列 LinearLayout(更简洁)
复杂定位、减少嵌套 ConstraintLayout
需要精确定位每个元素 ConstraintLayout

七、完整模板(可直接套用)

<TextView
    android:id="@+id/tvXxx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="内容"
    app:layout_constraintTop_toBottomOf="@id/上一个ID"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginTop="16dp" />

记忆口诀:Top 接 Bottom,Start+End 成对,marginTop 控间距。

2.插入图片

图片要水平居中就补 Start/End;姓名接图片下面要用 Top_toBottomOf 而不是 Bottom_toBottomOf

最简模板:

<ImageView
    android:id="@+id/ivPhoto"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:src="@drawable/photo"
    android:scaleType="centerCrop"
    android:contentDescription="照片"
    app:layout_constraintTop_toBottomOf="@id/上一个ID"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginTop="16dp" />

参数含义:

参数逐个中文说明

参数 中文含义 常用值
android:id 控件编号,给别的元素引用 @+id/ivPhoto
android:layout_width 宽度 120dp / wrap_content / match_parent
android:layout_height 高度 120dp / wrap_content
android:src 图片来源 @drawable/文件名
android:scaleType 图片缩放方式 见下表
android:contentDescription 图片内容描述(无障碍用,建议必加) "照片"
app:layout_constraintTop_toBottomOf 顶部接在谁的下面 @id/上一个ID
app:layout_constraintStart_toStartOf 左边缘对齐 parent(父容器)
app:layout_constraintEnd_toEndOf 右边缘对齐 parent(父容器)
android:layout_marginTop 与上方元素的间距 16dp

scaleType 缩放方式详解(重点)

中文效果 适用场景
centerCrop 裁剪填满,保持比例,超出部分裁掉 最常用,头像、封面图
fitCenter 完整显示,保持比例,四周留白 图片不能裁剪时
fitXY 拉伸填满,会变形 少用
center 原图大小居中,不缩放 小图标
centerInside 缩小到能完整显示 类似 fitCenter

口诀:头像用 centerCrop,要完整显示用 fitCenter**

微光录 · 免责声明 2026-09-07

评论区