1

我正在关注此android developer page以创建应用程序的自定义属性。一切都很顺利,我能够编译并看到结果。 但是问题的部分是IDE.Android Studio正在抱怨意外的命名空间自定义。有什么办法可以禁用/隐藏这个错误信息吗?这非常烦人。尝试使用自定义属性时发现标记片段的意外名称空间前缀“自定义”

但是,即使有这个错误,我也能编译和运行应用程序。

enter image description here

我跟着follwing步骤。

1)创建RES /价值/ attrs.xml文件,并添加

<?xml version="1.0" encoding="utf-8"?> <resources> 
<declare-styleable name="FragArguments"> 
    <attr name="max_allowed" format="integer"/> 
    <attr name="header" format="string"/> 
</declare-styleable> 

2)想在我mainlayout文件的片段标签

<LinearLayout xmlns:custom="http://schemas.android.com/apk/res" 
          android:layout_width="fill_parent" 
          android:layout_height="0dip" 
          android:layout_weight="1"> 
       <fragment android:id="@+id/msg" 
          android:name="org.android.fragment.MessageFragment" 
          custom:max_allowed="85" 
          custom:header="@string/header" 
          android:layout_width="wrap_content" 
          android:layout_height="match_parent"/> 
      </LinearLayout> 

3.应用使用自定义属性通过代码覆盖片段的onInflate()方法

@Override 
    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { 
     super.onInflate(activity, attrs, savedInstanceState); 

     TypedArray typedArray = activity.obtainStyledAttributes(attrs, R.styleable.FragArguments); 
     max_allowed = typedArray.getInt(R.styleable.FragArguments_max_allowed, -1); 
     header = typedArray.getString(R.styleable.FragArguments_header); 
     typedArray.recycle(); 
    } 

回答

5

我刚想出解决方案。

问题ID MissingPrefix告诉皮棉只扫描那些丢失了Android命名空间prefix.So我们能做到这两个方面XML属性: -

  1. Canissue以下命令扫描的文件在主项目目录及其子目录下。

棉绒--check MissingPrefix MYPROJECT

  • 在Android Studio中创建具有以下内容的lint.xml文件,并添加在应用程序的目录。
  • <?xml version="1.0" encoding="utf-8"?> 
    <lint> 
        <issue id="MissingPrefix" severity="ignore"/> 
    </lint> 
    
    1

    感谢@Nicks答案。您还可以将以下行添加到android {}范围内的build.gradle文件中:

    lintOptions { 
         disable 'MissingPrefix' 
        }