Android踩坑记录:This view is not constrained vertically

老代码中的ListView移植到新的ads时,发现布局文件有异常,百度后解决,增加一行即可:

app:layout_constraintTop_toTopOf="parent"

布局代码全部:

    <ListView
        android:id="@+id/lvMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1px"
        app:layout_constraintTop_toTopOf="parent"
          />

java代码:

......

 ListView lv = (ListView) findViewById(R.id.lvMain);

        String[] data = {"sfdsf", "fadf", "fasdf"};

        ArrayAdapter<String> adt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);

        lv.setAdapter(adt);
......

 

 

这个报错是 Android Studio 的 **ConstraintLayout 约束警告**: > ❌ `This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint` 意思是:你用了 `ConstraintLayout`,但某个 View 没有设置 **垂直方向的约束(top/bottom/baseline)**,在运行时可能会“跳到顶部”,布局行为不可控。 --- ### ✅ 问题场景 你在 `activity_route_plan.xml` 中添加了如下 `TextView`: ```xml <TextView android:id="@+id/tv_route_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="12dp" android:background="#F0F8FF" android:text="正在规划路线..." android:textSize="14sp" android:textColor="#333" android:gravity="center" /> ``` 但 **没有给它任何上下(vertical)约束**,所以 ConstraintLayout 不知道它应该放在哪里 → 报错! --- ## ✅ 解决方案:为 `tv_route_info` 添加完整约束 以下是 **推荐的修改方式**,确保它: - 距离顶部有一定间距 - 宽度铺满 - 不与其他组件重叠 ### ✅ 修改后的完整代码片段(ConstraintLayout 内) ```xml <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/tv_route_info" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <TextView android:id="@+id/tv_route_info" android:layout_width="0dp" android:layout_height="wrap_content" android:padding="12dp" android:background="#F0F8FF" android:text="正在规划路线..." android:textSize="14sp" android:textColor="#333" android:gravity="center" app:layout_constraintBottom_toTopOf="@id/recycler_steps" app:layout_constraintTop_toBottomOf="@id/map_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_steps" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_route_info" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` --- ### 🔍 关键修复点解释: | 属性 | 作用 | |------|------| | `app:layout_constraintTop_toBottomOf="@id/map_view"` | 当前 TextView 在地图下方 | | `app:layout_constraintBottom_toTopOf="@id/recycler_steps"` | TextView 在 RecyclerView 上方 | | `app:layout_constraintStart/End_toStart/EndOf="parent"` | 左右撑满 | | `android:layout_width="0dp"` | 在 ConstraintLayout 中,`0dp` 表示“根据约束自动拉伸” | > ⚠️ 注意:在 `ConstraintLayout` 中,**必须用 `0dp` 配合约束**,不能写 `match_parent`(虽然能运行,但不推荐) --- ## ✅ 最终布局结构逻辑 ``` [ MapView ] ← 占顶部大部分 [ tv_route_info ] ← 显示路线信息(持续) [ recycler_steps ] ← 显示详细步骤(可滚动) ``` 所有组件都有完整的上下左右约束,不再报错 ✅ --- ## 🛠 小贴士:如何避免这类错误? 1. **使用 Design 视图拖拽控件时,Android Studio 会自动加约束** 2. **手动写 XML 时,每个 View 必须至少有一个 vertical + horizontal 约束** 3. **看到黄色感叹号或红色波浪线要及时处理** --- ✅ 现在你的 `TextView` 不再报错,且: - 布局稳定 - 运行正常 - 适配各种屏幕 直接复制上面 XML 片段替换即可!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值