解析XML时,未绑定前缀的含义是什么?

Par*_*rth 13 android-layout

我已经在我的Android应用程序中为自定义小部件创建了一个xml文件,错误是:

解析XML时出错:未绑定前缀

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.CustomWidget.MyView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        android:id="@+id/surface"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_weight="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

com.example.CustomWidget是我的包MyView的名称,是我创建自定义的类文件的名称Widget.

小智 24

第二个XML命名空间是正确的,但它是自定义窗口小部件的命名空间,因此您需要适当地定义名称空间:

xmlns:android="http://schemas.android.com/apk/res/android"
Run Code Online (Sandbox Code Playgroud)

变为:

xmlns:mynamespace="http://schemas.android.com/apk/res/com.myproject.myprojectname"
Run Code Online (Sandbox Code Playgroud)

此后,您为自定义视图定义的任何自定义属性元素将被称为:

mynamespace:my_defined_attribute="success"
Run Code Online (Sandbox Code Playgroud)

代替 :

android:layout_width="fill_parent"
Run Code Online (Sandbox Code Playgroud)


Ari*_*ael 5

在XML文件中,您必须添加一行:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.myproject.winedeals"
 .....
 .....>
Run Code Online (Sandbox Code Playgroud)


Blu*_*ell 0

取出第二个 XML 命名空间声明:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <com.example.CustomWidget.MyView
     android:id="@+id/surface
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" 
     android:layout_weight="1"
     android:focusable="true"
     android:focusableInTouchMode="true"  />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)