【问题标题】:"No resource found" When using <include layout="">“未找到资源”当使用 <include layout="">
【发布时间】:2013-01-15 03:29:13
【问题描述】:

在我的项目中,我创建了一个全局标题布局 global_header.xml,并通过 &lt;include layout="@layout/global_header.xml"&gt; 在我的所有布局 XML 文件中使用它。

这个方法我以前用过,目前在这个项目中使用。我的问题是我的布局包含以下内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF" >

    <include layout="@layout/global_header" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/global_header">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

global_header.xml的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/global_header"
    android:layout_width="fill_parent"
    android:layout_height="60sp"
    android:layout_alignParentTop="true"
    android:background="#FFDDDDDD" >

    <ImageView
        android:id="@+id/global_header_logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5sp"
        android:layout_marginTop="5sp"
        android:adjustViewBounds="true"
        android:src="@drawable/header" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1sp"
        android:layout_alignParentBottom="true"
        android:background="#FF000000" />

</RelativeLayout>

我在android:layout_below="@global/header" 上收到一条错误消息:

错误:错误:未找到与给定名称匹配的资源(位于“layout_below”,值为“@global/header”)。

我已经在项目的其他布局中使用了包含,它可以正常工作,但无论出于何种原因,此布局文件都不会像所有其他布局一样从标题加载 ID。

即使我确信一旦在设备上运行它会发现它没有问题,该项目也不会出现此错误,是否有其他人遇到此问题?有解决方案/解决方法吗?

非常感谢任何和所有帮助。

【问题讨论】:

    标签: android xml layout include


    【解决方案1】:

    试试这个,指定包含布局的id并使用

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF" >
    
    <include layout="@layout/global_header"  android:id="@+id/header"/>
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/header">
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </RelativeLayout>
    </ScrollView>
    

    【讨论】:

    • 它对我有用.. 我已经为include layout 指定了 ID。然后使用 layout_below 它为我工作
    • 还要检查这个答案:stackoverflow.com/q/11668718/1306012 它说“问题是你必须在第一次提到每个 id 时使用@+id/,然后再使用@id/,即使是第一次可能不是一个定义。”
    【解决方案2】:

    希望 xml (main.xml &amp; global_header.xml) 都存在于资源中的布局文件夹中。

    请清理项目并重新启动您的 Eclipse,它可以工作。

    注意@我检查了你的 xmls 我没有收到任何错误。

    【讨论】:

    • 这两件事我都做了。我以前用随机文件遇到过这个问题,唯一的解决方案是将数据复制到每个丑陋的文件中。现在我已经解决了这个问题通过使用&lt;include layout="@layout/global_header" android:id="@+global/header"/&gt; 并从头文件中取出ID。
    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 2013-06-22
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多