简体   繁体   English

如何将导航抽屉包含到我的XML布局文件中

[英]How Can I Include Navigation Drawer into My XML Layout File

I have xml layout main_activity.xml and the root layout element is LinearLayout. 我有xml布局main_activity.xml,根布局元素是LinearLayout。 I want to include navigation drawer in my application. 我想在我的应用程序中包含导航抽屉。 so I created a menu.xml and header.xml layout file. 所以我创建了menu.xml和header.xml布局文件。 Then I included the code for the drawer into my activity_main.xml. 然后,将抽屉的代码包含在我的activity_main.xml中。 but it is not working. 但它不起作用。 I think it might be b/c the root element needs to be DrawerLayout. 我认为可能是b / c,根元素需要为DrawerLayout。 But if I change LinearLayout to DrawerLayout, then my xml is all messed up. 但是,如果我将LinearLayout更改为DrawerLayout,那么我的xml就会一团糟。 It can't find the listview or make sense of the views in the page. 它找不到列表视图或无法理解页面中的视图。 I have include the appropiate plugins in my gradle file in order to add DrawerLayout. 我已经在我的gradle文件中包含了适当的插件,以便添加DrawerLayout。 How can I include navigation drawer in my app. 如何在应用程序中包含导航抽屉。 Thanks main_activity.xml file 感谢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>

header file 头文件

<?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>

menu file 菜单文件

<?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>

Have you add this to your activity ? 您将此添加到您的活动了吗?

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();

EDIT : You right you have to use a Drawer Activity, you could use an "include" in your layout like this 编辑:您正确,您必须使用Drawer Activity,您可以在布局中使用“包含”,例如这样

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM