【问题标题】:"Unbound prefix" error in layout xml file布局 xml 文件中的“未绑定前缀”错误
【发布时间】:2015-07-18 06:00:40
【问题描述】:

我在activity_main.xml 文件的app: 行上收到“unbound prefix”的 XML 解析错误

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    app:rb_color="#0099CC"
    >
 ...

rb_colorres/values/attrs.xml 中定义如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RippleBackground">
        <attr name="rb_color" format="color" />
        <attr name="rb_strokeWidth" format="dimension"/>
        <attr name="rb_radius" format="dimension"/>
        <attr name="rb_duration" format="integer"/>
        <attr name="rb_rippleAmount" format="integer"/>
        <attr name="rb_scale" format="float"/>
        <attr name="rb_type" format="enum">
            <enum name="fillRipple" value="0"/>
            <enum name="strokeRipple" value="1"/>
        </attr>
    </declare-styleable>
</resources>

我正在遵循here 的指示。

但是由于波纹代码是作为单个 java 文件实现的,而不是将波纹代码构建为库然后将库合并到我的项目中,我只是将波纹源 java 文件直接放入我的项目中。

大概我需要将应用程序:更改为其他内容以引用rb_color 来设置它,但是该怎么做呢?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您必须在父视图中添加以下行:

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

    像这样

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

    【讨论】:

    • 谢谢。如果父视图是最外层的视图,这将起作用,就像在我的代码发布中一样。但是,如果我尝试在主 RelativeLayout 中添加另一个嵌入的 RelativeLayout 并将 app: 行添加到嵌套的 RelativeLayout 中,那么即使我将 xmlns 行添加到嵌套的布局中,它也不再起作用。为什么会这样?
    • 无论你的 app:attribute 在哪里,始终将 xmlns:app 放在第一个视图中(最外层)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2021-04-12
    • 2012-12-22
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多