重力没有发现抽屉图左侧
当我运行的应用程序,然后单击工具栏上的图标,我得到的错误是“重力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);
内容和抽屉必须是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>
内容和抽屉应该在不同的布局,抽屉应该有重力了。
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>
首先设置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)
这将抽屉带到右侧,但菜单(汉堡包图标)仍然正确。你可以请帮助 – Vinay 2016-11-08 05:50:18
你可以发布你的XML文件。 – 2016-11-08 06:38:19
它可能试图接近抽屉
mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.Start)) {
mDrawerLayout.closeDrawer(Gravity.Start);
} else {
mDrawerLayout.openDrawer(Gravity.Start);
}
}
更多参考相关的方式
https://developer.android.com/reference/android/support/v4/view/GravityCompat.html#START
U可以这样http://*.com/questions/30706552/没有更多的允许打开抽屉布局从右侧在导航抽屉/ 30738746#30738746 谢谢:) – ssPerman01 2015-06-09 17:25:29
可能重复[Android DrawerLayout - 没有抽屉视图与重力找到](http://*.com/questions/27350136/android-drawerlayout-no-drawer-view-found-with重力) – 2Dee 2015-07-22 22:40:21