2017-06-24 24 views
3

ナビゲーションドロワ機能を自分のアクティビティに追加したいが、右にスワイプするとデフォルトのメニューアイコンが表示されない。ここに私のレイアウトファイルだ:ツールバーのメニューアイコンがDrawerLayoutに表示されない理由

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appBarLayout" 
      android:layout_width="match_parent" 
      android:theme="@style/AppTheme.AppBarOverlay" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" 
       app:layout_scrollFlags="scroll|enterAlways" /> 
      <android.support.design.widget.TabLayout 
       android:id="@+id/tabLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:tabTextColor="@android:color/white" 
       app:tabSelectedTextColor="@android:color/white" 
       app:tabIndicatorColor="@android:color/white" 
       app:tabIndicatorHeight="6dp"/> 
     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_one_more" 
     app:menu="@menu/activity_one_more_drawer" /> 

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

My活動ファイル:

public class PartThreeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 


    private DrawerLayout mDrawerLayout; 
    private Toolbar toolbar; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     setTheme(R.style.AppThemeBlue); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_part_three); 

     initToolbar(); 
     initViewPagerAndTabs(); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    private void initToolbar() { 
     Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 
     setTitle(getString(R.string.app_name)); 
     mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); 
    } 

    private void initViewPagerAndTabs() { 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); 
     PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); 
     pagerAdapter.addFragment(PartThreeFragment.createInstance(20), getString(R.string.tab_1)); 
     pagerAdapter.addFragment(PartThreeFragment.createInstance(4), getString(R.string.tab_2)); 
     viewPager.setAdapter(pagerAdapter); 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
     tabLayout.setupWithViewPager(viewPager); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.one_more, 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. 
     int id = item.getItemId(); 

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

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    static class PagerAdapter extends FragmentPagerAdapter { 

     private final List<Fragment> fragmentList = new ArrayList<>(); 
     private final List<String> fragmentTitleList = new ArrayList<>(); 

     public PagerAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      fragmentList.add(fragment); 
      fragmentTitleList.add(title); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return fragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return fragmentList.size(); 
     } 
     @Override 
     public CharSequence getPageTitle(int position) { 
      return fragmentTitleList.get(position); 
     } 
    } 
} 

そして、ここではスクリーンショットです:ここでは enter image description here enter image description here

は私のプロジェクトへのリンクです: https://drive.google.com/open?id=0B6_hNGJUq8_2RUtxTl9pTGlqb2c 助けてください私はなぜメニューが表示されていない、おかげで見つける。

答えて

2
Toolbar mToolbar現在このへ

変更この

private void initToolbar() { 
     Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 
     setTitle(getString(R.string.app_name)); 
     mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); 
    } 

が初期化されていないtoogleを追加しながら、あなたがtoolbar参照を使用しているので、効果がありませんローカル変数があるので、あなたのtoolbar参照が初期化されていません

private void initToolbar() { 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     //^^^^ initialize the reference which is being used to add toggle 
     setSupportActionBar(toolbar); 
     setTitle(getString(R.string.app_name)); 
     toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); 
    } 
関連する問題