我正在尝试将 TextView 放置在带有左侧抽屉的主要 Activity 中。可以毫无问题地放置 Textview,但是当我在 Textview 中添加 Android:id 时,它会抛出错误:
Android.Views.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class android.support.design.internal.NavigationMenuItemView.
此代码:有效
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
tools:showIn="@layout/app_bar_main"
android:text="Hello World!" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
但是这段代码没有
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
tools:showIn="@layout/app_bar_main"
android:id="@+id/textView1"
android:text="Hello World!" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
我只添加了一行代码
android:id="@+id/textView1"
最佳答案
当您的 appcompat 库和设计支持库的版本不匹配时,可能会出现此问题。
compile 'com.android.support:appcompat-v7:23.1.1' // appcompat library
compile 'com.android.support:design:23.1.1' //design support library
关于Android:id 在 DrawerLayout 内容 View 中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359584/