问题描述
|
我在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>
com.example.CustomWidget
是我的程序包的名称,MyView
是我在其中创建自定义Widget
的类文件的名称。
解决方法
第二个XML名称空间是正确的,但是它是自定义窗口小部件的名称空间,因此您需要适当地定义名称空间:
xmlns:android=\"http://schemas.android.com/apk/res/android\"
变成:
xmlns:mynamespace=\"http://schemas.android.com/apk/res/com.myproject.myprojectname\"
此后,您为自定义视图定义的任何自定义属性元素将称为:
mynamespace:my_defined_attribute=\"success\"
代替 :
android:layout_width=\"fill_parent\"
, 在您的XML文件中,您必须添加一行:
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
xmlns:app=\"http://schemas.android.com/apk/res/com.myproject.winedeals\"
.....
.....>
, 取出第二个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>