2015-01-04 105 views
1

我跟着這StackOverflow post的指示,並設法讓ActionBarDrawerToggle及其動畫工作。但是,沒有導航抽屜滑出。這是爲什麼?材質設計抽屜不滑出

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" 
tools:ignore="MergeRootFrame" 
android:fitsSystemWindows="true" > 

<include layout="@layout/toolbar" /> 

<it.neokree.materialtabs.MaterialTabHost 
    android:id="@+id/materialTabHost" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    app:textColor="#FFFFFF" 
    app:primaryColor="#33B5E5" 
    app:accentColor="#FFFFFF" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

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

    <!-- Main layout --> 
    <FrameLayout 
     android:id="@+id/main_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- Nav drawer --> 
    <fragment 
     android:id="@+id/nav_drawer" 
     android:name="com.wsandhu.conjugation.DrawerFragment" 
     android:layout_width="@dimen/drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="left|start" /> 
</android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

下面是MainActivity.javaonCreate()方法的代碼:

ActionBarDrawerToggle mDrawerToggle; 
DrawerLayout mDrawerLayout; 

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

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

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name); 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

... 

    if (savedInstanceState == null) { 
     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       getSupportFragmentManager().beginTransaction() 
         .add(R.id.container, new MainFragment()) 
         .commit(); 
      } 
     }); 
    } 

... 

} 

我有一個DrawerFragment類和它的XML文件,但作爲我說,當我在我的應用程序中點擊ActionBarDrawerToggle時,沒有任何東西滑出去。切換動畫完美。

如另一篇文章中所述,需要被覆蓋的五種方法在我的MainActivity中,在onCreate()方法之後。我真的不知道還有什麼要補充的,我無法弄清楚。

在此先感謝。

回答

0

我不明白DrawerLayout,部分原因是由於jpardogo's的答案。

我需要設置我的活動的XML文件正確的主要內容框架,所以我做了android.support.v4.widget.DrawerLayout文件的根元素,並把我的活動導航抽屜上方的LinearLayoutfragment元素。這是正確的工作代碼:

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

<!-- Main layout --> 
<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    tools:ignore="MergeRootFrame" 
    android:fitsSystemWindows="true" > 

    <include layout="@layout/toolbar" /> 

    <it.neokree.materialtabs.MaterialTabHost 
     android:id="@+id/materialTabHost" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     app:textColor="#FFFFFF" 
     app:primaryColor="#33B5E5" 
     app:accentColor="#FFFFFF" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<!-- Nav drawer --> 
<fragment 
    android:id="@+id/nav_drawer" 
    android:name="com.wsandhu.conjugation.DrawerFragment" 
    android:layout_width="@dimen/drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="left|start" /> 

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

這將有助於將它與原始文章中的XML文件進行比較。希望這可以幫助。