我有xml布局main_activity.xml,而根布局元素是LinearLayout。我想在我的应用程序中包含导航抽屉。所以我创建了menu.xml和header.xml布局文件。然后,将抽屉的代码包含在我的activity_main.xml中。但它不起作用。我认为可能是b / c,根元素需要为DrawerLayout。但是,如果我将LinearLayout更改为DrawerLayout,那么我的xml就会一团糟。它找不到列表视图或无法理解页面中的视图。我已经在我的gradle文件中包含了适当的插件,以便添加DrawerLayout。如何在应用程序中包含导航抽屉。谢谢 main_activity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayout_main"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".activity.MainActivity">
<!--custome toolbar-->
<include layout="@layout/tool_bar" />
<!--Wifi name and state-->
<LinearLayout
android:id="@+id/linear_wifi_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:gravity="left"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_wifi_img"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="vertical"
>
<TextView
android:id="@+id/wifi_lable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:gravity="left"
android:layout_gravity="center_vertical"
android:textColor="@color/colorAccent"
android:text="WIFI: " />
<TextView
android:id="@+id/ip_lable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="left"
android:layout_gravity="center_vertical"
android:textColor="@color/colorAccent"
android:text="IP: " />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:orientation="vertical"
>
<TextView
android:id="@+id/wifi_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="left"
android:layout_gravity="center_vertical"
android:textColor="@color/textColor"
android:text="PORTTECH" />
<TextView
android:id="@+id/ipaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="left"
android:layout_gravity="center_vertical"
android:textColor="@color/textColor"
android:text="192.168.1.29/255.255.255.0" />
</LinearLayout>
</LinearLayout>
<!--Progess bar-->
<ProgressBar
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:id="@+id/progress_bar" />
<TextView
android:id="@+id/result_local"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:text="Local Network:"
android:paddingLeft="5dp"
android:textColor="@color/colorAccent"
android:textSize="18sp"/>
<ListView
android:id="@+id/local_network"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
android:fitsSystemWindows="true"
android:id="@+id/nav_drawer">
</android.support.design.widget.NavigationView>
<RelativeLayout android:layout_width="fill_parent"
android:id="@+id/ad_layout"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_alignBottom="@+id/home_layout">
<ImageView
android:id="@+id/scan_button"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@drawable/round_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_next_button"
android:layout_marginRight="10dp"/>
</RelativeLayout>
</LinearLayout>
头文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_header"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="@color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/nav_header_imageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Netdroid"
android:textSize="22dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
菜单文件
<?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">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
</menu>
答案 0 :(得分:1)
您已将其添加到活动中吗?
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
编辑: 正确,您必须使用“抽屉活动”,您可以在布局中使用“包含”
<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/content_visualisation"
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_visualisation"
app:menu="@menu/activity_visualisation_drawer"/>
</android.support.v4.widget.DrawerLayout>