2015-12-09 6 views
5

ツールバーにナビゲーションドロワーを表示しようとしています。私が思うように、私はすでに必要なものはすべて書きましたが、ツールバーには何も表示されません。テキストのみ動物園表示用のアイコンではないnavbarDrawerLayoutナビゲーションドロワーが表示されない

私はアンドロイドで新しいです。私は何かが欠けている場合私は驚きではありません。

はここにここに私の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); 

はまた、代わりに、トグルの5パラメータのコンストラクタを使用して、第三として、あなたのToolbarオブジェクトを渡すことができます引数。この場合

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

、あなたはサポートActionBarset*()通話、およびトグルにonOptionsItemSelected(item)チェックを省略することができます。これは、ToolbarがサポートActionBarに設定されていない場合に便利です。しかし、documentationは、Toolbarがそのように設定されている場合、4パラメータコンストラクタ、したがって現在の設定を推奨します。

関連する問題