前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【错误记录】约束布局报错 ( Missing Constraints in ConstraintLayout. This view is not constrained. It only has )

【错误记录】约束布局报错 ( Missing Constraints in ConstraintLayout. This view is not constrained. It only has )

作者头像
韩曙亮
发布2023-03-30 19:22:08
8370
发布2023-03-30 19:22:08
举报

文章目录

一、报错信息


约束布局中 , 如果不给组件添加约束 , 就会报如下错误 :

代码语言:javascript
复制
Missing Constraints in ConstraintLayout
This view is not constrained. It only has designtime positions, 
so it will jump to (0,0) at runtime unless you add the constraints  
The layout editor allows you to place widgets anywhere on the canvas, 
and it records the current position with designtime attributes (such as layout_editor_absoluteX). 
These attributes are not applied at runtime, so if you push your layout on a device, 
the widgets may appear in a different location than shown in the editor. 
To fix this, make sure a widget has both horizontal and vertical 
constraints by dragging from the edge connections.  
Issue id: MissingConstraints  Vendor: Android Open Source Project Contact: 
https://groups.google.com/g/lint-dev 
Feedback: https://issuetracker.google.com/issues/new?component=192708  
在这里插入图片描述
在这里插入图片描述

二、解决方案


1、手动添加约束 ( 推荐 )

Design 界面拖动添加约束操作 : 手动拖动 布局上下左右的 约束按钮 , 添加约束 ;

在这里插入图片描述
在这里插入图片描述

Code 代码添加约束 : 在代码中 , 添加 如下 上下左右的 约束 ;

app:layout_constraintBottom_toTopOf=“@+id/guideline3” app:layout_constraintEnd_toEndOf=“parent” app:layout_constraintStart_toStartOf=“parent” app:layout_constraintTop_toTopOf=“parent”

组件约束完整示例如下 :

代码语言:javascript
复制
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView1"
        android:name="kim.hsl.livedatademo.Fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

2、自动添加约束

使用 Design 面板中的 Infer Constraints 工具 , 可以自动为组件添加约束 ;

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、报错信息
  • 二、解决方案
    • 1、手动添加约束 ( 推荐 )
      • 2、自动添加约束
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档