LayOut_1

本文详细介绍了界面布局的概念,包括布局的两种实现方式,基本属性如match_parent、dp、sp、padding、margin等,以及常见布局类型如LinearLayout、RelativeLayout的使用方法和特性。通过案例深入解析了padding与margin的区别,LinearLayout中layout_weight的作用,以及帧布局的特点。

1. 什么是布局

就是把界面中的控件按照某种规律摆放到指定的位置

2. 布局的二种实现

代码
xml配置文件:res/layout目录下
注:也可以同时使用xml和代码

3. 布局的基本属性

取值范围
{ //效果是一样的
fill_parent//过时
match_parent//不过时
}

固定值
{
dp 控件大小
sp 文字大小
}

padding 内补丁
margin 外补丁

android:gravity和android:layout_gravity
用一个TextView、文字、背景查看效果最方便
android:gravity:控件内部的元素
android:layout_gravity:控件所在父元素的位置
但父元素的水平和垂直设置的优先度更高

4. 常见布局

线性布局(重点) LinearLayout
表格布局(几乎不用)
帧布局

绝对布局
相对布局 RelativeLayout
网格布局
RTL(几乎不用)

5. 案例1:padding内补丁、margin外补丁的区别,以及与内容

match_parent/fill_parent
padding/margin
background
wrap_content

6. 案例2:线性布局(重点)

orientation=”vertical|horizontal”
android:gravity:控件内部的元素(对内有效)

7. 案例3:android:gravity和android:layout_gravity的区别

7.1 android:gravity:控件内部的元素(对内有效)
android:layout_gravity:控件所在父元素的位置(对外生效)
7.2 特殊情况
父元素的android:orientation=”vertical”,layout_gravity 水平设置的相关属性有效
父元素的android:orientation=”horizontal”,layout_gravity 垂直设置的相关属性有效

8. 案例4:LinearLayout布局中Layout_weight属性的作用

8.1 首先声明只有在Linearlayout中,layout_weight属性才有效。
8.2 它是用来指定(剩余空闲空间)的分割比例,而非按比例分配整个空间。
8.3 另外android:layout_weight会引起争议,是因为在设置该属性的同时,
设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。
8.4 如果想用layout_weight平均分配空间,正确方式是将layout_width(或layout_height)设置为0dp,
再通过layout_weight按比例分配空间
注:关键点在于搞清楚什么是剩余空闲空间

9. 案例5:帧布局(就好象一张张卡片堆叠上去,后面会盖出前面的)

FrameLayout
注:帧布局有点类似于awt的CardLayout都是把组件一个一个叠加到一起,
    但CardLayout能将下面的组件移上来,但FrameLayout没有提供此功能

ImageView 
  src
  scaleType="fitXY" XY方向拉伸

 android:scaleType="fitXY"
        android:src="@drawable/img18"
<?xml version="1.0" encoding="utf-8"?> <com.tplink.design.card.TPConstraintCardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp" android:orientation="vertical"> <!-- 第一部分:顶部横条,占总高度约 1/3 --> <View android:layout_width="match_parent" android:layout_height="20dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:layout_marginTop="8dp" android:background="@drawable/skeleton_rounded_background" /> <!-- 第二部分:中间区域,占 1/3,两边 margin 16dp --> <View android:layout_width="match_parent" android:layout_height="34dp" android:layout_marginStart="12dp" android:layout_marginEnd="12dp" android:layout_marginTop="16dp" android:background="@drawable/skeleton_rounded_background" /> <!-- 第三部分:多个 item 列表,每个 item 左右留 16dp 边距 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="8dp"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toBottomOf="@id/view1" /> <View android:id="@+id/view4" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toBottomOf="@id/view1" /> <View android:id="@+id/view4" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toBottomOf="@id/view1" /> <View android:id="@+id/view4" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toBottomOf="@id/view1" /> <View android:id="@+id/view4" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <View android:id="@+id/view1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toBottomOf="@id/view1" /> <View android:id="@+id/view4" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout> </LinearLayout> </com.tplink.design.card.TPConstraintCardView> 把这几个变成不同的id发给我
最新发布
12-12
你当前的 XML 中,多个 `ConstraintLayout` 内部的子 View(如 `image`, `view1`, `view2`, `view3`, `view4`, `divider`)都使用了**相同的 ID**,这在 Android 布局中是 **不推荐甚至可能导致绑定错误或 findViewById 找到错误视图的问题**,尤其是在同一个父布局下。 --- ✅ **目标:为每一个 `ConstraintLayout` 中的控件设置唯一 ID,避免重复 ID 引发问题** 我们可以通过 **添加数字后缀的方式命名 ID**,例如: - `image_0`, `image_1`, `image_2`... - `view1_0`, `view1_1`... 这样即使你在 Java/Kotlin 代码中通过 `findViewById()` 查找也不会出错。 --- ### ✅ 修改后的关键点 我们将每个 `ConstraintLayout` 块中的: ```xml android:id="@+id/image" → 改为 → android:id="@+id/image_0"、"@+id/image_1" ... ``` 对 `view1`, `view2`, `view3`, `view4`, `divider` 同样处理。 --- ### ✅ 修改后示例(仅展示前两个 item,其余类推) ```xml <!-- 第一个 item --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image_0" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <View android:id="@+id/view1_0" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image_0" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2_0" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3_0" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image_0" app:layout_constraintTop_toBottomOf="@id/view1_0" /> <View android:id="@+id/view4_0" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2_0" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4_0" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3_0" /> </androidx.constraintlayout.widget.ConstraintLayout> <!-- 第二个 item --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:id="@+id/image_1" android:layout_width="@dimen/tpds_all_dp_40" android:layout_height="@dimen/tpds_all_dp_40" android:layout_marginStart="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <View android:id="@+id/view1_1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintStart_toEndOf="@id/image_1" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view2_1" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_12" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:background="@drawable/skeleton_rounded_background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/view3_1" android:layout_width="@dimen/tpds_all_dp_165" android:layout_height="@dimen/tpds_all_dp_16" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/image_1" app:layout_constraintTop_toBottomOf="@id/view1_1" /> <View android:id="@+id/view4_1" android:layout_width="73dp" android:layout_height="@dimen/tpds_all_dp_18" android:layout_marginTop="@dimen/tpds_all_dp_8" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_12" android:background="@drawable/skeleton_rounded_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/view2_1" /> <com.tplink.design.divider.MaterialDivider android:id="@+id/divider_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@+id/view4_1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/view3_1" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` > 🔁 对剩余的第3、第4、第5个 item,请依次将 `_0` → `_2`, `_3`, `_4` 替换即可。 --- ### ✅ 完整建议命名规则 | 控件类型 | 命名方式 | 示例 | |----------------|------------------|--------------------| | 图像 | `image_N` | `image_0`, `image_1` | | 标题文字块 | `view1_N` | `view1_0`, `view1_1` | | 副标题/状态文字 | `view2_N` | `view2_0` | | 内容文本块 | `view3_N` | `view3_2` | | 数值/标签 | `view4_N` | `view4_2` | | 分割线 | `divider_N` | `divider_3` | 其中 `N = 0,1,2,3...` 表示第几个 item。 --- ### ✅ 为什么这么做? | 原因 | 说明 | |------|------| | 🚫 避免 ID 冲突 | 多个同 ID 的 View 在 `findViewById()` 时只返回第一个匹配项 | | ✅ 更安全的数据绑定 | 如果使用 ViewBinding 或 DataBinding,唯一 ID 更清晰 | | ✅ 易于调试 | 日志或 UI Inspector 中能准确识别具体是哪个 item | | ✅ 符合规范 | 虽然 Android 允许复用 ID(用于模板),但在静态布局中应尽量唯一 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值