navigation MenuItem in toolbar doesn't trigger onOptionsItemSelected

前端 未结 3 1640
野趣味
野趣味 2021-01-23 18:18

I\'m trying to use the navigation drawer with toolbar, but pressing the navigation button doesn\'t trigger the onOptionsItemSelected handler and the list doesn\'t open.

相关标签:
3条回答
  • 2021-01-23 18:45

    This is what worked for me:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="match_parent" tools:context=".MainActivity">
    
        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            />
    
        <android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tool_bar">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <com.augury.mobile.auguryandroid.SlidingTabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:elevation="2dp"
                    android:background="@color/augury_blue"/>
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:layout_weight="1">
                </android.support.v4.view.ViewPager>
    
            </LinearLayout>
    
            <!-- The navigation drawer -->
            <ListView android:id="@+id/left_drawer"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:background="#111"/>
    
        </android.support.v4.widget.DrawerLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-23 18:46

    Try this layout:

    <android.support.v4.widget.DrawerLayout
       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"
       tools:context=".MainActivity"
       >
    
       <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
       >
    
      <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
    
      <com.augury.mobile.auguryandroid.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/augury_blue"/>
    
      <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1">
      </android.support.v4.view.ViewPager>
    
    </LinearLayout>
    
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
    
    </android.support.v4.widget.DrawerLayout>
    
    0 讨论(0)
  • 2021-01-23 18:46

    write this

      getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    just after you set your toolbar

    and remove

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    this will take you to previous task if you have any which might give you an unexpected behavior

    0 讨论(0)
提交回复
热议问题