android - 找不到与给定名称匹配的资源?

标签 android xml android-studio android-relativelayout

这让我发疯。我的项目刚才编译得很好。我在其他地方做了一些小改动,现在我收到了这个错误。这是完整的错误消息:

no resource found that matches the given name (at 'layout_above' with value '@id/blank_view").

这是发生错误的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="@dimen/margin_right"
        android:layout_above="@id/blank_view"
        android:src="@drawable/button" />

    <View
        android:id="@+id/blank_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/margin_bottom"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

当该资源在文件中就在它下面时,它怎么会找不到名称为“@id/blank_view”的资源?

顺便说一句,我之所以有这样的布局是因为我希望 ImageButton 与我的相关布局的底部对齐,但要向上偏移一个边距。出于某种原因,这两个属性(layout_alignParentBottomlayout_marginBottom)在同一 View 中不能很好地混合。

我还应该指出,这也发生在早些时候,但我只是删除了给 AndroidStudio 带来此类问题的引用,而不是尝试修复它。然而,这太重要了,不能就这样挥手而去。

最佳答案

发生这种情况是因为 xml 以线性方式解析,并且 View /对象以从上到下的顺序创建。因此,您的 xml 告诉 View 构建器将您的 ImageButton 放在尚不存在的项目之上。

只需将它移到空白 View 下方,错误就会消失。

关于android - 找不到与给定名称匹配的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189550/

相关文章:

android - 为什么使用 Android 附近共享时通过蓝牙传输文件?

android - 如何使用git跟踪android分支

android - Phonegap 检测 backButton 事件失败

C# XMLreader 和子节点

python - Openerp-如何删除图像上的编辑选项?

android - 为什么自动完成在 android.support.v7.widget.CardView 标签中不起作用?

android - 如何在 Android 中绘制自定义条形图

SQL Server 2008 查找和替换 XML 列中的元素

android - 尝试删除 app/build/intermediates/manifests/full/debug/output.json 或清理构建目录

android - 如何在 Android Studio 中配置 Git 可执行文件的路径?