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

Aut*_*pps 2 xml android relativelayout android-studio

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

找不到与给定名称匹配的资源(在'layout_above'中,值为'@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>
Run Code Online (Sandbox Code Playgroud)

如果该资源位于文件的正下方,那怎么能找不到名为"@ id/blank_view"的资源呢?

顺便说一句,我有这样的布局的原因是我希望ImageButton它与我的相对布局的底部对齐,但是偏移了一个边距.出于某种原因,这两个属性(layout_alignParentBottomlayout_marginBottom)在同一视图中不能很好地混合.

我还应该指出,这也发生在早期,但我刚刚删除了给AndroidStudio这样一个问题的引用,而不是试图修复它.然而,这太重要了,不能那样挥之不去.

Ree*_*Rob 8

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

只需将其移动到空白视图下方,错误就会消失.