在自定义视图中为标记找到意外的命名空间前缀 "app",但它实际上是正确的
Unexpected namespace prefix "app" found for tag in custom view, but it is actually right
这里有很多与“为标签 找到意外的命名空间前缀“app””相关的问题,但我没有找到与我的具体问题相匹配的问题。这发生在我自己创建的名为 SelfieHudView 的自定义视图中,它在一个项目中运行良好,但是当我将完全相同的布局复制到其他项目时,我开始遇到那些讨厌的错误,并且看不到在布局编辑器中绘制的视图设计。此外,该项目按预期构建,所以我认为它可能是一些 Android Studio 故障,尽管我尝试了通常的“无效缓存和重新启动”东西但没有修复它。
所以我的自定义视图是这样声明的:
import androidx.appcompat.widget.AppCompatImageView;
public class SelfieHudView extends AppCompatImageView {
// ...
}
我在每个项目的 res/values 文件夹中有一个 selfie_hud_attrs.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SelfieHudView">
<attr name="strokeColorNeutral" format="color" />
<attr name="strokeColorGood" format="color" />
<attr name="strokeColorBad" format="color" />
<attr name="fillingColorNeutral" format="color" />
<attr name="fillingColorGood" format="color" />
<attr name="fillingColorBad" format="color" />
<attr name="ovalRatio" format="boolean" />
<attr name="selfieState" format="enum">
<enum name="NEUTRAL" value="0"/>
<enum name="GOOD" value="1"/>
<enum name="BAD" value="2"/>
</attr>
</declare-styleable>
</resources>
在这两种情况下,布局文件中的父布局都是这样的:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="my.package.SelfieRegistrationFragment">
唯一改变的是每个项目不同的“my.package”,其他的都是一样的。
<SelfieHudView
android:id="@+id/selfie_hud_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="16dp"
app:fillingColorBad="#80C8C8C8"
app:fillingColorGood="#80C8C8C8"
app:fillingColorNeutral="#80C8C8C8"
app:layout_constraintBottom_toTopOf="@+id/take_photo_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:strokeColorBad="@color/red"
app:strokeColorGood="@color/green"
app:strokeColorNeutral="@color/lightgray" />
在原来的项目中,它按预期工作,但在我尝试重用它的新项目中,它抱怨所有这些应用程序中的前缀“Color “ 属性。不出所料,如果我按照建议将它们替换为 android:,它将无法构建并出现属性未找到错误。如果我将它们留给 app: 另一方面,它构建得非常好,但 AS 中的预览不起作用。
有什么想法吗?
谢谢!
您必须为自定义视图指定完整的程序包名称。如果包是“com.example”,则指定:
<com.example.SelfieHudView
... />
...或...
<view
class="com.example.SelfieHudView"
... />
那个“视图”必须全部小写。我找不到很好的参考资料,但网上有很多该技术的示例。
这里有很多与“为标签 找到意外的命名空间前缀“app””相关的问题,但我没有找到与我的具体问题相匹配的问题。这发生在我自己创建的名为 SelfieHudView 的自定义视图中,它在一个项目中运行良好,但是当我将完全相同的布局复制到其他项目时,我开始遇到那些讨厌的错误,并且看不到在布局编辑器中绘制的视图设计。此外,该项目按预期构建,所以我认为它可能是一些 Android Studio 故障,尽管我尝试了通常的“无效缓存和重新启动”东西但没有修复它。
所以我的自定义视图是这样声明的:
import androidx.appcompat.widget.AppCompatImageView;
public class SelfieHudView extends AppCompatImageView {
// ...
}
我在每个项目的 res/values 文件夹中有一个 selfie_hud_attrs.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SelfieHudView">
<attr name="strokeColorNeutral" format="color" />
<attr name="strokeColorGood" format="color" />
<attr name="strokeColorBad" format="color" />
<attr name="fillingColorNeutral" format="color" />
<attr name="fillingColorGood" format="color" />
<attr name="fillingColorBad" format="color" />
<attr name="ovalRatio" format="boolean" />
<attr name="selfieState" format="enum">
<enum name="NEUTRAL" value="0"/>
<enum name="GOOD" value="1"/>
<enum name="BAD" value="2"/>
</attr>
</declare-styleable>
</resources>
在这两种情况下,布局文件中的父布局都是这样的:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="my.package.SelfieRegistrationFragment">
唯一改变的是每个项目不同的“my.package”,其他的都是一样的。
<SelfieHudView
android:id="@+id/selfie_hud_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:padding="16dp"
app:fillingColorBad="#80C8C8C8"
app:fillingColorGood="#80C8C8C8"
app:fillingColorNeutral="#80C8C8C8"
app:layout_constraintBottom_toTopOf="@+id/take_photo_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:strokeColorBad="@color/red"
app:strokeColorGood="@color/green"
app:strokeColorNeutral="@color/lightgray" />
在原来的项目中,它按预期工作,但在我尝试重用它的新项目中,它抱怨所有这些应用程序中的前缀“Color “ 属性。不出所料,如果我按照建议将它们替换为 android:,它将无法构建并出现属性未找到错误。如果我将它们留给 app: 另一方面,它构建得非常好,但 AS 中的预览不起作用。
有什么想法吗?
谢谢!
您必须为自定义视图指定完整的程序包名称。如果包是“com.example”,则指定:
<com.example.SelfieHudView
... />
...或...
<view
class="com.example.SelfieHudView"
... />
那个“视图”必须全部小写。我找不到很好的参考资料,但网上有很多该技术的示例。