一、報(bào)錯(cuò)信息
約束布局中 , 如果不給組件添加約束 , 就會(huì)報(bào)如下錯(cuò)誤 :
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、手動(dòng)添加約束 ( 推薦 )
Design 界面拖動(dòng)添加約束操作 : 手動(dòng)拖動(dòng) 布局上下左右的 約束按鈕 , 添加約束 ;
Code 代碼添加約束 : 在代碼中 , 添加 如下 上下左右的 約束 ;
app:layout_constraintBottom_toTopOf=“@+id/guideline3”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent”
組件約束完整示例如下 :
<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、自動(dòng)添加約束
使用 Design 面板中的 Infer Constraints 工具 , 可以自動(dòng)為組件添加約束 ;文章來源:http://www.zghlxwxcb.cn/news/detail-480065.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-480065.html
到了這里,關(guān)于【錯(cuò)誤記錄】約束布局報(bào)錯(cuò) ( Missing Constraints in ConstraintLayout. This view is not constrained. It only has )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!