Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在活动中使用菜单功能设置抽屉_Android_Navigation Drawer_Drawertoggle - Fatal编程技术网

Android 如何在活动中使用菜单功能设置抽屉

Android 如何在活动中使用菜单功能设置抽屉,android,navigation-drawer,drawertoggle,Android,Navigation Drawer,Drawertoggle,我必须为我的应用程序设置抽屉布局,因此我已完成活动中的布局和功能。现在,我无法使用碎片。如何使用活动中的菜单功能设置抽屉?请帮助我。这是我的主要布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"`enter

我必须为我的应用程序设置抽屉布局,因此我已完成活动中的布局和功能。现在,我无法使用碎片。如何使用活动中的菜单功能设置抽屉?请帮助我。这是我的主要布局

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"`enter code here`
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.2"
        android:background="@color/MediumSeaGreen"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:id="@+id/notification_settings"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.85"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_action_overflow" />
        <TextView
            android:id="@+id/mytrip_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:text="@string/Notification"
            android:textSize="@dimen/mytrip_title"
            android:textStyle="bold" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.8"
        android:orientation="horizontal"
        android:weightSum="1" >
        <ListView
            android:id="@+id/notification_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>
</LinearLayout>  

这是我为抽屉设计的布局

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"`enter code here`
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.2"
        android:background="@color/MediumSeaGreen"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:id="@+id/notification_settings"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.85"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_action_overflow" />
        <TextView
            android:id="@+id/mytrip_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:text="@string/Notification"
            android:textSize="@dimen/mytrip_title"
            android:textStyle="bold" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.8"
        android:orientation="horizontal"
        android:weightSum="1" >
        <ListView
            android:id="@+id/notification_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>
</LinearLayout>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000"
    android:paddingRight="10dp"
    android:layout_weight="8"
    android:layout_marginLeft="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
      >

        <ImageView
            android:id="@+id/menu_home_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_home_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Home"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/menu_Trips_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_trips_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Trips"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/menu_notification_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_notification_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Notification"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/menu_Settings_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_settings_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Settings"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/menu_profile_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_profile_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Profile"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/menu_friends_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
           android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />

        <TextView
            android:layout_marginLeft="10dp"
            android:id="@+id/menu_friends_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Friends"
            android:textSize="@dimen/mytrip_title" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"`enter code here`
        android:background="@color/gray" />

</LinearLayout>     

单击主布局中的通知设置图像视图时,我必须查看下面为抽屉功能设计的布局,请帮助我

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"`enter code here`
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.2"
        android:background="@color/MediumSeaGreen"
        android:orientation="horizontal"
        android:weightSum="1">

        <ImageView
            android:id="@+id/notification_settings"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.85"
            android:padding="@dimen/menu_icon_padding"
            android:src="@drawable/ic_action_overflow" />
        <TextView
            android:id="@+id/mytrip_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:text="@string/Notification"
            android:textSize="@dimen/mytrip_title"
            android:textStyle="bold" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.8"
        android:orientation="horizontal"
        android:weightSum="1" >
        <ListView
            android:id="@+id/notification_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>
</LinearLayout>  
  • 将当前活动布局替换为以下内容:

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"`enter code here`
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.2"
            android:background="@color/MediumSeaGreen"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <ImageView
                android:id="@+id/notification_settings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.85"
                android:padding="@dimen/menu_icon_padding"
                android:src="@drawable/ic_action_overflow" />
            <TextView
                android:id="@+id/mytrip_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="@string/Notification"
                android:textSize="@dimen/mytrip_title"
                android:textStyle="bold" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1.8"
            android:orientation="horizontal"
            android:weightSum="1" >
            <ListView
                android:id="@+id/notification_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>
    </LinearLayout>  
    
    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_drawer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    
    
    <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    // put your existing widgets here
    
    </FrameLayout>
    
    
    <ListView
    android:id="@+id/drawer_menu"
    android:layout_width="define your width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:layout_weight="1"
    />
    
    
    //将现有的小部件放在这里
    

  •    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"`enter code here`
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.2"
            android:background="@color/MediumSeaGreen"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <ImageView
                android:id="@+id/notification_settings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.85"
                android:padding="@dimen/menu_icon_padding"
                android:src="@drawable/ic_action_overflow" />
            <TextView
                android:id="@+id/mytrip_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="@string/Notification"
                android:textSize="@dimen/mytrip_title"
                android:textStyle="bold" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1.8"
            android:orientation="horizontal"
            android:weightSum="1" >
            <ListView
                android:id="@+id/notification_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>
    </LinearLayout>  
    
  • 在活动中实例化抽屉布局、菜单列表、操作栏和切换

  •    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"`enter code here`
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.2"
            android:background="@color/MediumSeaGreen"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <ImageView
                android:id="@+id/notification_settings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.85"
                android:padding="@dimen/menu_icon_padding"
                android:src="@drawable/ic_action_overflow" />
            <TextView
                android:id="@+id/mytrip_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="@string/Notification"
                android:textSize="@dimen/mytrip_title"
                android:textStyle="bold" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1.8"
            android:orientation="horizontal"
            android:weightSum="1" >
            <ListView
                android:id="@+id/notification_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>
    </LinearLayout>  
    
  • 为列表定义合适的行布局并设置数据。您可以从这里轻松找到示例代码

  •    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"`enter code here`
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.2"
            android:background="@color/MediumSeaGreen"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <ImageView
                android:id="@+id/notification_settings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.85"
                android:padding="@dimen/menu_icon_padding"
                android:src="@drawable/ic_action_overflow" />
            <TextView
                android:id="@+id/mytrip_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="@string/Notification"
                android:textSize="@dimen/mytrip_title"
                android:textStyle="bold" />
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1.8"
            android:orientation="horizontal"
            android:weightSum="1" >
            <ListView
                android:id="@+id/notification_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>
    </LinearLayout>  
    

    你希望在抽屉里放什么?所以,谢谢你的回复,当点击菜单按钮时,它就像菜单一样打开抽屉,并显示该抽屉中的其他活动标题。类似这样的内容??是的,但我不能使用片段类谢谢你的编码,请告诉我如何在我的活动中实例化抽屉布局,我对此一无所知,请帮忙。试试这里,这是很好的解释,没关系,你只需要看看他们的主要活动。片段或活动与实例化操作栏、切换、抽屉布局等无关。然后单击菜单后,您只需调用相关活动的意图,而不是调用片段:)只需使用trydisplayView方法选择用户想要去的活动,这意味着我必须使用intent,在这种情况下,我必须将字符串和图标的列表放在同一个活动本身中,而不是放在seprate字符串中,这就是我想要做的