2015-12-09 104 views
5

我試圖在我的工具欄中顯示導航抽屜。我想我已經寫了它所需要的一切,但是沒有任何東西顯示在工具欄中。只有文字動物園。不顯示導航欄的圖標DrawerLayout抽屜不顯示

我是新來的android,如果我失去了一些東西,它不會讓我吃驚。

這裏是我的MainActivity.java

package com.example.zoo; 

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mActionBarDrawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_Layout); 
     mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed){ 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if (getSupportActionBar() != null) { 
        getSupportActionBar().setTitle(R.string.drawer_opened); 
       } 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if (getSupportActionBar() != null) { 
        getSupportActionBar().setTitle(R.string.drawer_closed); 
       } 
      } 
     }; 

     mDrawerLayout.setDrawerListener(mActionBarDrawerToggle); 
    } 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mActionBarDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     if(mActionBarDrawerToggle.onOptionsItemSelected(item)) 
      return true; 

     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

這裏是我的content_main.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" 
tools:context="com.example.gsiradze.zoo.MainActivity" 
android:orientation="vertical"> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?android:attr/actionBarSize"/> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_Layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <ListView 
     android:id="@+id/drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white"/> 

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

</LinearLayout> 

,當然,我的這些字符串名稱

<resources> 
    <string name="drawer_opened">Select an item</string> 
    <string name="drawer_closed">Zoo</string> 
</resources> 
+0

你寫信打開導航抽屜的地方? –

+0

@MikeM。你可以發佈這個答案。感謝隊友 – gsiradze

+0

@MikeM。其實兩者都是正確的。謝謝 – gsiradze

回答

3

的問題是這條線:

getSupportActionBar().setDisplayShowHomeEnabled(true); 

爲了您的當前設置,它應該是:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

你也,而是使用了五參數構造函數的切換,並通過您的Toolbar對象作爲第三論據。

mActionBarDrawerToggle = new ActionBarDrawerToggle(this, 
                mDrawerLayout, 
                toolbar, 
                R.string.drawer_opened, 
                R.string.drawer_closed) {...}; 

在這種情況下,你可以省略支持ActionBarset*()電話,和onOptionsItemSelected(item)檢查的切換。如果您的Toolbar沒有設置爲支持ActionBar,這很有用。但是,documentation建議使用四參數構造函數,因此建議使用當前設置(如果Toolbar設置爲這樣)。