【发布时间】:2021-08-03 09:27:17
【问题描述】:
这里有很多“Unexpected namespace prefix "app" found for tag”相关问题,但我没有找到一个与我的具体问题相匹配的问题。这发生在我自己创建的名为 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" />
在原始项目中它按预期工作,但在我试图重用它的新项目中,它抱怨 app: 所有这些“Color”属性中的前缀。不出所料,如果我用 android: 替换这些,正如 AS 所建议的那样,它将无法使用属性未找到错误构建。如果我将它们留给 app: 另一方面,它构建得非常好,但 AS 中的预览不起作用。
有什么想法吗?
谢谢!
【问题讨论】:
-
您尝试过清理和重建您的项目吗?自定义视图名称 SelfieHudView 是 XML 文件中的完整包名吗?
-
@Cheticamp 尝试了前几次无济于事,以及通常的无效缓存内容,但第二次就在现场!您介意将其发布为答案,以便我投票并选择正确的答案吗?
标签: java android android-studio android-layout