我想要包含一个包含2-3个固定项目的菜单,这些项目永远不会更改,在菜单下方我想要包含一个我可以动态管理的ListView
。
我想做类似下面的图片。
当我启动应用程序时,我总是收到以下错误:
Process: com.pigmalionstudios.todolist, PID: 7055
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class menu
MainActivity.java
中的相应错误行是:
setContentView(R.layout.activity_main);
这里是activity_main.xml
布局。
<?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">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header_main" />
<include layout="@menu/drawer_menu"/>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:textColor="#424242"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view"
android:id="@+id/activity_main_drawer"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:orientation="vertical">
<group android:checkableBehavior="single">
<item
android:id="@+id/add"
android:icon="@drawable/add"
android:title="@string/add" />
<item
android:id="@+id/ToDo"
android:icon="@drawable/pending"
android:title="To Dos" />
<item
android:id="@+id/settings"
android:icon="@drawable/settings"
android:title="Settings" />
</group>
</menu>
答案 0 :(得分:0)
您不必在导航抽屉中获得所需布局的Menu
。只需在导航栏中添加标题视图和ListView
即可。
就我而言,我只需要为ListView
创建自定义标头,然后在导航栏中添加标题和ListView
。这个过程可能看起来像这样。
LinearLayout nDrawerListHeader = (LinearLayout) inflater.inflate(R.layout.header_navigation_drawer, container, false);
mDrawerListView.addHeaderView(nDrawerListHeader);
header_navigation_drawer.xml
看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/navigation_header_color_white">
<!-- Some other layouts to get the precise view -->
</RelativeLayout>
</LinearLayout>
然后通过在标题视图中找到正确的视图来实现动作点击。
Button menuButton = nDrawerListHeader.findViewById(R.id.menu_button_1);
menuButton.setOnClickListener(...);
希望有所帮助!