找不到元素 'android.support.constraint.ConstraintLayout' 的声明

Cannot find the declaration of element 'android.support.constraint.ConstraintLayout'

我创建了一个名为 activity_main(sw50dp) 的 xml 文件,但是当我尝试验证它时,它给出了错误:

Error:(4, 42) cvc-elt.1.a: Cannot find the declaration of element 'android.support.constraint.ConstraintLayout'.

C:\Program Files\Android\Android Studio\lib\idea.jar!\standardSchemas\xhtml1.dtd

Error:(26, 3) The markup in the document preceding the root element must be well-formed.

activity_main 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/images"
tools:context=".MainActivity">

<Button
    android:id="@+id/buttonDiv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="320dp"
    android:layout_marginTop="340dp"
    android:background="@drawable/circle"
    android:text="÷"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="220dp"
    android:layout_marginStart="100dp"
    android:background="@drawable/oval"
    android:text="2"
    app:layout_constraintLeft_toRightOf="@+id/button1"
    app:layout_constraintStart_toEndOf="@+id/button1"
    app:layout_constraintTop_toTopOf="@+id/button1" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="320dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="118dp"
    android:background="@drawable/oval"
    android:text="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/buttonReset"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="320dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="45dp"
    android:background="@drawable/oval"
    android:text="C"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textNum"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="100dp"
    android:layout_marginStart="100dp"


<Button
    android:id="@+id/button5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="220dp"
    android:layout_marginStart="100dp"
    android:background="@drawable/oval"


</android.support.constraint.ConstraintLayout>

activity_main(sw50dp) 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

我还创建了其他布局文件,例如 activity_main(normal-land)。我对他们没有任何问题。

注意:如果资源目录名称不正确,可能会出现此错误。

创建目录名的最佳步骤:

  1. 正在执行:文件 -> 新建 -> Android 资源目录
  2. 选择:最小屏幕宽度
  3. 点击“>>”并输入[浸入次数]

此外,将此行添加到 Gradle 文件可解决类似的错误:


dependencies {
    ...
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
}

随后重新同步。

Constraint Layout Versions

Google Constraint Layout Sample

对于这种情况,您可以添加行

工具:忽略="MissingDefaultResource"

到布局目录中的布局文件的 xml 代码,它应该忽略由于缺少 运行 应用程序

不需要的资源而产生的错误