android - 此处不允许使用元素按钮
问题描述
我在资源/布局文件夹中创建了一个 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>
解决方案
如果您没有在 gradle 文件中添加 androidx ConstraintLayout 库作为依赖项,则会发生这种情况。
在您的 app/build.gradle 文件的dependencies
块中,添加以下内容:
dependencies {
// ...
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta3"
}
当然,您可以使用不同的库版本,但该androidx.constraintlayout:constraintlayout:
部分很重要。
完成后,进行 gradle 同步并重建您的项目,警告应该会消失。
推荐阅读
- c++ - 使用 basic_string 声明一种新的字符串类型
- python - 在数据框中应用功能缓慢
- c# - asp.net C# - 无法从 sql 数据库中填充(预选)下拉列表 SelectedValue
- java - 应用程序安装失败 安装失败并显示消息 Failed to finalize session : INSTALL_PARSE FAILED MANIFEST MALFORMED
- c++ - 如何在 C++ OpenCV 中使用 OutputArrayOfArrays
- go - Golang模板函数返回空白页
- visual-studio-code - VS Code 中的“在文件中搜索”
- javascript - 如何使最小高度为 300 像素的 ng-bootstrap 轮播响应?
- scala - scala spark数据框爆炸很慢-因此,替代方法-从列中的数组创建列和行
- c - 如何调试 Lua 扩展随机段错误