【问题标题】:no resource found that matches the given name?找不到与给定名称匹配的资源?
【发布时间】:2015-03-22 00:15:02
【问题描述】:

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

未找到与给定名称匹配的资源(位于“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>

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

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

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

【问题讨论】:

  • 尝试清理您的项目?

标签: android xml android-studio android-relativelayout


【解决方案1】:

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

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

【讨论】:

  • 或使用android:layout_above="@+id/blank_view"
  • 通常情况下,我在发布问题后不久就知道了这一点。但这是正确的答案,感谢您的回复。
【解决方案2】:

尝试将属性添加到您的视图中:

<view 
    android:id="@+id/blank_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/margin_bottom"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/btn" 
/>

然后删除:

android:layout_above="@id/blank_view"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多