【问题标题】:Unexpected namespace prefix “custom” found for tag fragment while trying to use custom attributes尝试使用自定义属性时,为标签片段找到了意外的命名空间前缀“自定义”
【发布时间】:2015-07-13 01:20:51
【问题描述】:

我正在关注android developer page 为应用程序创建自定义属性。一切正常,我也可以编译并查看结果。 但是出现问题的部分是 IDE.Android Studio 抱怨意外的命名空间自定义。有什么方法可以禁用/隐藏此错误消息?这很烦人。

不过,即使出现此错误,我也能够编译并运行该应用程序。

我遵循了以下步骤。

1) 创建 res/values/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();
    }

【问题讨论】:

    标签: android-layout android-studio android-custom-attributes


    【解决方案1】:

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

        lintOptions {
            disable 'MissingPrefix'
        }
    

    【讨论】:

      【解决方案2】:

      我刚刚想出了解决办法。

      问题 ID MissingPrefix 告诉 lint 只扫描缺少 Android 命名空间前缀的 XML 属性。所以我们可以通过两种方式做到这一点:--

      1. 可以发出以下命令扫描项目主目录及其子目录下的文件。

      lint --check MissingPrefix myproject

      1. 在 android studio 中创建一个 lint.xml 文件,内容如下,并添加到 app 目录中。
      <?xml version="1.0" encoding="utf-8"?>
      <lint>
          <issue id="MissingPrefix" severity="ignore"/>
      </lint>
      

      【讨论】:

        猜你喜欢
        • 2016-12-18
        • 2013-05-03
        • 2013-02-01
        • 2013-06-01
        • 2014-04-05
        • 2013-09-11
        • 1970-01-01
        • 1970-01-01
        • 2013-05-28
        相关资源
        最近更新 更多