0

我有一個DrawerLayout和AppBarLayout的問題。我需要添加DrawerLayout來製作NavigationDrawer。當我添加DrawerLayout時,整個佈局繪製成藍色。導航抽屜僅通過刷卡打開,不會通過單擊按鈕工作。我不明白問題是什麼。請幫幫我。DrawerLayout和AppBarLayout的問題

enter image description here

這是我的主要佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:expanded="false"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@color/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       app:tabMode="scrollable" /> 
     </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.NavigationView 
      android:id="@+id/nav_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:fitsSystemWindows="true" 
      app:menu="@menu/drawermenu" /> 
    </android.support.v4.widget.DrawerLayout> 
</android.support.design.widget.CoordinatorLayout> 

這是我的主要活動代碼:

package com.jeffe.tabstest; 

import android.content.Intent; 
import android.support.design.widget.NavigationView; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.view.ViewPager; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    private DrawerLayout drawerLayout; 

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

     toolbar = (Toolbar)findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

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

     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 
       R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawerLayout.addDrawerListener(toggle); 
     toggle.syncState(); 

     viewPager = (ViewPager)findViewById(R.id.viewPager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout)findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 

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

    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     drawerLayout.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     assert drawerLayout != null; 
     if (drawerLayout.isDrawerOpen(GravityCompat.START)) { 
      drawerLayout.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFragment(new MusicTab(), "Tab1"); 
     adapter.addFragment(new AnimeTab(), "Tab2"); 
     adapter.addFragment(new ArtistTab(), "Tab3"); 
     adapter.addFragment(new AlbumTab(), "Tab4"); 
     adapter.addFragment(new FavoritesTab(), "Tab5"); 
     viewPager.setAdapter(adapter); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

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

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 
} 

蹊蹺的佈局,因爲任何按鈕在AppBar沒有按沒有工作。但是在我添加DrawerLayout之前,一切都很完美。

+1

的'DrawerLayout'需要只有一個主要內容'View'。你有兩個,它像處理AppBarLayout一樣處理整個主要內容。您可以重新排列您的佈局,如[本帖]所示(http://stackoverflow.com/questions/30719369/android-design-library-coordinatorlayout-appbarlayout-and-drawerlayout)。 –

+0

謝謝,我只是移動AppBarLayout到另一個佈局文件,現在永久完美的作品。 –

回答

0

請勿使用ActionBarDrawerToggleNavigationView這是最古老的方法。

試試這個 -

toolbar = (Toolbar)findViewById(R.id.toolbar); 
toolbar.setNavigationIcon(YOUR_MENU_ICON); //set your navigation icon 
setSupportActionBar(toolbar); 


toolbar.setNavigationOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
      drawerLayout.openDrawer(drawerLayout); 
     } 
    }); 

希望這將有助於:)

+0

不起作用。關鍵仍然不起作用,但至少是NavigationView動畫 - 現在不是。藍屏呢?我認爲這與我的佈局有關,但我不明白。順便說一下,我也無法按標籤。 –

+0

沒有找到你?請詳細說明你的意思 –

+0

我的意思是我的佈局問題,因爲AppBar中沒有任何按鈕不起作用。 –