AIDE 中文文档 下载 App

布局属性编辑——让每个 View 大小和位置正确

核心布局属性

每个 Android View 都有两个最基本的布局属性:layout_width 和 layout_height。

属性值含义
match_parent填满父容器的可用空间
wrap_content尺寸刚好容纳内容
具体数值如 100dp、24sp、5px

密度无关像素(dp)

推荐用 dp(density-independent pixel)作为尺寸单位:

文字大小(sp)

文字大小推荐用 sp(scale-independent pixel):

边距与内边距

┌─── padding (内部填充) ───┐
│  content                  │
└─── padding (内部填充) ───┘
←─ margin (外部间距) →

两个属性都支持分别设置四个方向:

android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"

Gravity 与 Layout Gravity

容易混淆的两个属性:

属性作用对象效果
gravityView 自己View 内部内容的对齐方式
layout_gravity父容器View 在父容器中的对齐方式

例如:

常用属性速查表

属性作用示例
android:id给 View 一个唯一标识@+id/myButton
android:textTextView/Button 显示的文字"点击我"
android:textSize文字大小18sp
android:textColor文字颜色#FF0000 或 @color/red
android:background背景@drawable/shape 或 #FFFFFF
android:visibility可见性visible / invisible / gone
android:srcImageView 的图片@drawable/icon

AIDE 中设置属性的最快方式

  1. 输入 <Button 后按空格
  2. AIDE 自动弹出属性补全列表
  3. 选 android:layout_width → 弹出 match_parent / wrap_content
  4. 选 android:text → 输入文字值
  5. 自动补完引号和闭合,效率很高

如果有 UI Designer,以上所有属性都可以在右侧属性面板里修改,改完自动同步回 XML。