在Android中出现了意外的命名空间前缀“app”用于RelativeLayout标签?

19

我需要将 BottomSheetBehaviorScrollView 结合使用,但它提示了我:

Unexpected namespace prefix "app" found for tag RelativeLayout

app:behavior_hideable="true"
app:behavior_peekHeight="80dp"

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/ABLList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="end"
                    android:orientation="horizontal">

                    <ImageButton
                        android:id="@+id/imbDetail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end|center_vertical"
                        android:layout_marginRight="10dp"
                        android:background="@null"
                        android:src="@drawable/ic_action_detail" />
                </LinearLayout>
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_OrderList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/ABLList"
            android:scrollbars="vertical" />
    </RelativeLayout>

    <ScrollView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/layout_bottom_sheet_ListOrder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:background="@color/color_bottom_sheet"
            android:gravity="top"
            app:behavior_hideable="true"
            app:behavior_peekHeight="80dp"
            app:layout_behavior="@string/string_bottom_sheet_behavior">

            <com.example.asheq.utils.TextViewJus
                android:id="@+id/txtAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/text_marginTop"
                android:gravity="right"
                android:maxLines="5"
                android:paddingLeft="@dimen/activity_vertical_margin"
                android:paddingRight="@dimen/activity_vertical_margin"
                android:textSize="@dimen/text_size"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnSReport"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txtCTime"
                android:layout_marginTop="50dp"
                android:text="@string/btnReport" />

        </RelativeLayout>
    </ScrollView>

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/coordinatorOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
</android.support.design.widget.CoordinatorLayout>

8
如果你在textView上遇到了“Unexpected namespace prefix 'app' found”错误,你可能需要将TextView更改为android.support.v7.widget.AppCompatTextView - 林果皞
2
@林果皞 我认为你可以将其添加为答案,因为似乎你是唯一真正回答这个问题的人。 - Teng-pao Yu
7个回答

17

在布局中使用数据绑定时,此问题已广为人知。

假设您要使用带有app:前缀的数据绑定属性,则仅添加xmlns:app...是不够的。布局应该是数据绑定布局包装在<layout标签中

错误的方法:

例如,我导入了layout_toolbar_default.xml并使用app:toolbarTitle指定title

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        layout="@layout/layout_toolbar_default"
        app:toolbarTitle="@{@string/app_name}"
        />

</LinearLayout>

这将显示错误:发现意外的命名空间前缀“app”.

解决方案:

在您的布局中使用<layout标记包装,因为您正在使用绑定属性。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            layout="@layout/layout_toolbar_default"
            app:toolbarTitle="@{@string/app_name}"
            />

    </LinearLayout>
</layout>

这样做可以消除错误,但是如果该文件独立膨胀,则失败。 - Kenny
@Kenny 我没有理解 如果文件本身被压缩 的意思。 - Khemraj Sharma
当我在布局文件的顶层尝试使用<layout>,并直接膨胀该布局文件时,LayoutInflater.inflate会抛出异常,但当该布局用作include时,它可以正常工作。 - Kenny
你的意思是,不能直接填充数据绑定布局吗? - Khemraj Sharma
这解决了我的问题,不再崩溃,但现在即使其他所有内容都通过,它也不会在我的TextView中显示变量。 - Elliptica
显示剩余2条评论

6

我遇到了同样的问题。只需将底部工作表布局移到单独的文件中。然后通过 include 标签将其包含在主布局中即可。


6

使用 android.support.v7.widget.LinearLayoutCompat


此为android的LinearLayout兼容类,可以在旧版本的android系统中使用新版本LinearLayout布局样式。
<android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:behavior_hideable="true"
            app:behavior_peekHeight="80dp"
            app:layout_behavior="@string/string_bottom_sheet_behavior">

</android.support.v7.widget.LinearLayoutCompat>

2
现在最好使用AndroidX库。 - IgorGanapolsky

5

对于使用androidx的人,如果您从某个androidx视图中有子视图,则它们也应该是androidx视图,以能够识别应用程序命名空间前缀。例如,您应该使用<androidx.appcompat.widget.AppCompatImageView/>而不是<ImageView/>

这不会有任何危害,并且如果您使用了一些不受支持的功能(如ImageView的tints),它提供了对较低api版本的支持:)


2

如果你要多次使用 xmlns 命名空间,请尝试在父布局中设置一次。

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"

在父级CoordinatorLayout中编写此声明一次即可。
提示:在使用多个布局和视图时,请尝试实现<include/>属性。它将使您的代码干净且易于阅读。

1
对于遇到类似错误“Unexpected namespace prefix “app” found” 的人,在 textView 上,你可能需要将 TextView 更改为 android.support.v7.widget.AppCompatTextView

1
AppCompatTextView的文档说:“只有在编写自定义视图时才需要手动使用此类。” - ultraon
给那些点踩的人,你应该知道“you might”的意思(因为有自定义视图),而不是“you must”或“you should”。 - 林果皞

0
xmlns:app="http://schemas.android.com/apk/res-auto"

这个被设置了多次。在CoordinatorLayoutRelativeLayout中都有。 移除RelativeLayout中的一个。 在文件中声明一次就足够了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接