2017-10-11 10 views
-1

Ich verwende Navigation Drawer und die ActionBarDrawerToggle wird synchronisiert, wenn ich die Schublade durch Schieben öffne, aber wenn ich auf Toolbar Icon klicke, öffnet sich die Schublade nicht. Was ist falsch in meinem Code.Verknüpfen der Navigationsleiste mit der Werkzeugleistenschaltfläche

actionBarDrawerToggle = new ActionBarDrawerToggle(Main2Activity.this, drawerLayout, toolbar, R.string.drawer_open, R.string.close_drawer) { 
     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 
    }; 
    drawerLayout.setDrawerListener(actionBarDrawerToggle); 
    drawerLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      actionBarDrawerToggle.syncState(); 
     } 
    }); 

Dinge, die ich versuchen, so zu:

actionBar.setDisplayHomeAsUpEnabled(true); 
    @Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      drawerLayout.openDrawer(Gravity.START); 
      return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

Das ist mein Layout, die ich verwenden. Ich habe NavigationView und Schublade Layout.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context=".Main2Activity"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" 
       app:theme="@style/ToolbarColoredIcon" /> 
     <include layout="@layout/content_main2" /> 
    </RelativeLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

     <LinearLayout 
      android:id="@+id/nav_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <!--header--> 
      <include 
       android:id="@+id/header" 
       layout="@layout/nav_header" 

       /> 
      <!--items--> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingBottom="@dimen/dp20"> 

       <include 
        android:id="@+id/nav_menue_item_home" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_settings" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_more_apps" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_rate_us" 
        layout="@layout/nav_menue_item" /> 

       <include 
        android:id="@+id/nav_menue_item_privacy_policy" 
        layout="@layout/nav_menue_item" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#e3e4e3" 
       android:gravity="bottom" 
       android:orientation="vertical"> 

       <com.google.android.gms.ads.NativeExpressAdView 
        android:id="@+id/adViewNative" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        ads:adSize="@string/native_sm_ad_size" 
        ads:adUnitId="@string/native_sm_ad_unit_id" /> 

      </LinearLayout> 

     </LinearLayout> 
    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

so wenden Sie sich bitte, wo ist jetzt Problem?

Dank an alle habe ich mein Problem zu lösen. Das Problem ist nicht wirklich im Code, aber Problem ist im Layout, wo ich einen TabHost hinzufüge, der Symbolleiste für Klicken nicht erlaubt, so ist dieses tatsächliche Problem.

+0

Wenn 'toolbar' nicht null ist, dann sind der' setDisplayHomeAsUpEnabled() 'Aufruf und die' onOptionsItemSelected() 'Methode nicht notwendig mit diesem' ActionBarDrawerToggle' Konstruktor. Möglicherweise stimmt etwas mit dem Layout nicht. Sie haben z. B. eine 'View', die die' Toolbar' abdeckt. –

Antwort

1

erstellen toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:fitsSystemWindows="true" 
    > 

</android.support.v7.widget.Toolbar> 

erstellen drawer_menu.xml:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group 
     android:checkableBehavior="single" 
     android:id="@+id/first_group"> 

     <item 
      android:id="@+id/first_item" 
      android:title="First Item" 
      android:icon="@drawable/first_item" 
      /> 

    </group> 

    <group 
     android:checkableBehavior="single" 
     android:id="@+id/second_group"> 
     <item 
      android:id="@+id/second_item" 
      android:title="Second Item" 
      android:icon="@drawable/second_item"> 

     </item> 
    </group> 

</menu> 

Dann ist Ihre Aktivität Format so sein sollte:

<?xml version="1.0" encoding="utf-8"?> 
    <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" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     tools:context=".MainActivity"> 

     <!--First child represents elements of activity in drawerLayout--> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:id="@+id/relative_layout"> 

      <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" 
       /> 

     </RelativeLayout> 

     <!--Second child (navigation view) represents elements of navigation drawer in drawerLayout--> 

     <android.support.design.widget.NavigationView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/navigation_view" 
      android:layout_gravity="start" 
      android:background="@color/toolbar" 
      app:menu="@menu/drawer_menu"> 

     </android.support.design.widget.NavigationView> 

    </android.support.v4.widget.DrawerLayout> 

Für Hamburger Icon:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close); 

drawerLayout.addDrawerListener(actionBarDrawerToggle); 

Und dann, ein Verfahren erstellen:

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    actionBarDrawerToggle.syncState(); 
} 

Lassen Sie mich wissen, ob es hilft.

+0

Die Hamburger-Symbol funktionieren ordnungsgemäß, aber sie nicht, t öffnen Sie die Schublade dh nicht zu klicken Listener –

+0

Dann muss etwas mit Ihrem Layout nicht stimmen – Shaifu

+0

Überprüfen Sie die Layout-Änderungen @Shaifu –

Verwandte Themen