这让我发疯。我的项目刚才编译得很好。我在其他地方做了一些小改动,现在我收到了这个错误。这是完整的错误消息:
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_alignParentBottom
和 layout_marginBottom
)在同一 View 中不能很好地混合。
我还应该指出,这也发生在早些时候,但我只是删除了给 AndroidStudio 带来此类问题的引用,而不是尝试修复它。然而,这太重要了,不能就这样挥手而去。
最佳答案
发生这种情况是因为 xml 以线性方式解析,并且 View /对象以从上到下的顺序创建。因此,您的 xml 告诉 View 构建器将您的 ImageButton 放在尚不存在的项目之上。
只需将它移到空白 View 下方,错误就会消失。
关于android - 找不到与给定名称匹配的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189550/