android 自定义View报 error: No resource identifier found for attribute ‘XXX’ in package 'XXX'...

问题描述:


1328819-322549ec9749b76b.png
image.png

这个在某一个lib中,报错的代码:

<?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:orientation="vertical"
    android:layout_height="match_parent">

    <com.zhongjh.albumcamerarecorder.camera.CameraLayout
        android:id="@+id/cameraLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:duration_max="10000"
        app:iconMargin="20dp"
        app:iconSize="30dp"
        app:iconSrc="@drawable/ic_camera" />

</LinearLayout>

报错原因:
按照描述信息是找不到这些自定义字段,可以看到这个

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

res-auto自动识别,那么让我们来精确这个自定义属性,是来源于哪个命名空间的,改成

xmlns:app="http://schemas.android.com/apk/lib/com.zhongjh.albumcamerarecorder"

请注意,com.zhongjh.albumcamerarecorder是来源于AndroidManifest的package

编译后,还是不行,后来想到可能由于大环境的冲突,导致app标签识别不了,继续进一步修改成这样

xmlns:acr="http://schemas.android.com/apk/lib/com.zhongjh.albumcamerarecorder"

编译通过。

最终代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:acr="http://schemas.android.com/apk/lib/com.zhongjh.albumcamerarecorder"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <com.zhongjh.albumcamerarecorder.camera.CameraLayout
        android:id="@+id/cameraLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        acr:duration_max="10000"
        acr:iconMargin="20dp"
        acr:iconSize="30dp"
        acr:iconSrc="@drawable/ic_camera" />

</LinearLayout>

tips:假设出现以下问题


1328819-7dfba328952bb2b0.png
image.png

请记得给这些属性添加format属性

猜你喜欢

转载自blog.csdn.net/weixin_34258838/article/details/87021078