在布局文件里添加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.v2hi.top/?p=47