首页 > 解决方案 > 此处不允许使用元素按钮

问题描述

我在资源/布局文件夹中创建了一个 fragment_b.xml 文件。在我的两个按钮上,我收到以下警告:

此处不允许使用元素按钮

为什么会弹出这个警告?我的代码工作正常。

片段_b.xml:

<?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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <Button
            android:id="@+id/btn_fragment_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/go_to_fragment_c"
            app:layout_constraintBottom_toTopOf="@id/btn_nagivate_to_fragment_f"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <Button
            android:id="@+id/btn_nagivate_to_fragment_f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/go_to_fragment_f"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/btn_fragment_b"
            android:layout_marginTop="@dimen/a_lot_of_margin"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmllayoutandroid-resources

解决方案


如果您没有在 gradle 文件中添加 androidx ConstraintLayout 库作为依赖项,则会发生这种情况。

在您的 app/build.gradle 文件的dependencies块中,添加以下内容:

dependencies {
    // ...
    implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta3"
}

当然,您可以使用不同的库版本,但该androidx.constraintlayout:constraintlayout:部分很重要。

完成后,进行 gradle 同步并重建您的项目,警告应该会消失。


推荐阅读