2015-04-05 37 views
2

當我運行的應用程序,然後單擊工具欄上的圖標,我得到的錯誤是「重力LEFT沒有發現抽屜視圖」重力沒有發現抽屜圖左側

這是我的xml文件

主.XML

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/book_list_rv_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 

     <ListView 
      android:id="@+id/ListView1" 
      android:layout_width="241dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="#333" 
      android:choiceMode="singleChoice" 
      android:divider="#666" 
      android:dividerHeight="1dp" 
      android:paddingLeft="15sp" 
      android:paddingRight="15sp" /> 
    </LinearLayout> 

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

此標識的活動的java文件的代碼片段 活動文件

Toolbar mToolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     // drawer 
     setSupportActionBar(mToolbar); 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       mToolbar, R.string.app_name, R.string.app_name); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
+0

U可以這樣http://stackoverflow.com/questions/30706552/沒有更多的允許打開抽屜佈局從右側在導航抽屜/ 30738746#30738746 謝謝:) – ssPerman01 2015-06-09 17:25:29

+0

可能重複[Android DrawerLayout - 沒有抽屜視圖與重力找到](http://stackoverflow.com/questions/27350136/android-drawerlayout-no-drawer-view-found-with重力) – 2Dee 2015-07-22 22:40:21

回答

5

內容和抽屜必須是DrawerLayout的兩個孩子。所以改變你的佈局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/book_list_rv_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 

    </LinearLayout> 

    <ListView 
     android:id="@+id/ListView1" 
     android:layout_width="241dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#333" 
     android:choiceMode="singleChoice" 
     android:divider="#666" 
     android:dividerHeight="1dp" 
     android:paddingLeft="15sp" 
     android:paddingRight="15sp" /> 

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

內容和抽屜應該在不同的佈局,抽屜應該有重力了。

DrawerLayout

Contnet

抽屜

/DrawerLayout

更新您的文件中像這樣,

<LinearLayout .... 
<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/book_list_rv_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 
     </LinearLayout> 

     <ListView 
      android:id="@+id/ListView1" 
      android:layout_width="241dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="left" //or start 
      android:background="#333" 
      android:choiceMode="singleChoice" 
      android:divider="#666" 
      android:dividerHeight="1dp" 
      android:paddingLeft="15sp" 
      android:paddingRight="15sp" /> 
    </LinearLayout> 
5

首先設置drawerlayout,然後設置支持操作欄。 檢查下面的代碼。

Toolbar mToolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       mToolbar, R.string.app_name, R.string.app_name); 
mDrawerLayout.setDrawerListener(mDrawerToggle); 
setSupportActionBar(mToolbar);//modified(changed line) 
+0

這將抽屜帶到右側,但菜單(漢堡包圖標)仍然正確。你可以請幫助 – Vinay 2016-11-08 05:50:18

+0

你可以發佈你的XML文件。 – 2016-11-08 06:38:19