Android 在ConstraintLayout错误的约束部分定义id(@+id)

Android 在ConstraintLayout错误的约束部分定义id(@+id),android,android-constraintlayout,Android,Android Constraintlayout,第一次在约束部分(如app:layout_constraintBottom_toTopOf i get)中定义id@+id时,错误无法解析符号 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/androi

第一次在约束部分(如app:layout_constraintBottom_toTopOf i get)中定义id@+id时,错误无法解析符号

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@+id/tv2" />

    <TextView
        android:id="@id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>
应用程序运行正常,没有任何错误,但错误显示在android studio布局窗口中

使缓存无效、重建和更改约束布局版本这些都没有帮助

在Java类中,R.id.tv2工作正常,当我控制并单击它时,我可以在R类中看到它的字段。编辑器正在为ID使用另一个与java代码中的R类不同的类


这个问题是关于旧Android Studio中的一个bug,以及使用androidx库的当前版本的Android Studio中的ConstraintLayout版本。这个bug没有出现。

如其他答案中所述,使用@+id/声明新资源

在我的例子中,Android Studio抱怨在名称中使用-字符,即使应用程序以任何方式编译和运行。

在定义id@+id/tv2时添加了加号+,但在引用时,不需要像这样添加加号@id/tv2

而不是这个

  <?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@+id/tv2" />

    <TextView
        android:id="@id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>
使用以下命令:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@id/tv2" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>
为了正确,我建议您将代码转换为Androidx

下面的代码是用Androidx编写的

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/tv2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这个问题是关于旧Android Studio中的一个bug,以及使用androidx库的当前版本的Android Studio中的ConstraintLayout版本。这个bug没有出现。

尝试交换@+id/tv2和@id/tv2的值。将带+的放在下面的文本视图中。@Xenolion id的第一次出现应该是@+而不是second@Xenolion我现在交换了它,第二个文本视图是绿色的,但第一个仍然是红色的,这很奇怪,但我也开始怀疑你的Android Studio。@Xenolion此项目目标sdk是27我更新Android Studio并更改gradle插件到3.2.1,然后我必须将构建工具从27.0.3更改为28.0.3是否有任何错误?必须添加+符号的位置是您在代码中看到id名称的第一行,因为app:layout_constraintBottom_totototopof=@+id/tv2高于android:id=@id/tv2 app:layou_constraintBottom_topof=@+id/tv2应该有加号。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/tv2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>