1. 首页
  2. bug人生

在布局文件里添加toolbar时出现的错误error: Error parsing XML: unbound prefix

        最近要做一个关于小学科学的数字化实验应用,由于ActionBar已经不建议使用(在代码上被画了横线),于是改用Android 5.0新出的ToolBar。要使用ToolBar,就要自己在布局里添加一个ToolBar控件。下面是要加入的ToolBar代码。

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="@string/app_name"
        app:subtitle="@string/app_subtitle"
        app:navigationIcon="@drawable/home_ico"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"/>


然后就报错:

error: Error parsing XML: unbound prefix

解决方法是:

android:id=”@+id/toolbar”
下面添加一个属性:

xmlns:app=”http://schemas.android.com/apk/res/com.example.toolbardemo”
加入布局的toolbar为

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        xmlns:app="http://schemas.android.com/apk/res/com.example.toolbar"
        app:title="@string/app_name"
        app:subtitle="@string/app_subtitle"
        app:navigationIcon="@drawable/home_ico"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"/>

至此错误消失。

原创文章,作者:维尼兔,如若转载,请注明出处:https://www.v2v0.com/?p=47

发表评论

邮箱地址不会被公开。 必填项已用*标注

联系我们

 

工作时间:周一至周五,09:30-17:00,节假日休息