当我点击我的抽屉切换时,我得到以下异常
java.lang.IllegalArgumentException:没有发现重力的抽屉视图 LEFT
这是我的activity_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="@layout/fragment_navigation" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我的fragment_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
</ListView>
我的清单项目
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/navigation_item_text"
android:layout_gravity="center_horizontal" />
</LinearLayout>
答案 0 :(得分:19)
来自文档
要使用DrawerLayout,请将主要内容视图定位为第一个宽度和高度为match_parent的子视图。在主内容视图后添加抽屉作为子视图,并相应地设置layout_gravity。抽屉通常使用match_parent作为具有固定宽度的高度。
<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/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<fragment
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:name="com.xyz.ui.navigation.NavigationFragment"
tools:layout="@layout/fragment_navigation" />
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:13)
确定。让我在这里简单明了。
什么是DrawerLayout
? https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
DrawerLayout
充当允许的窗口内容的顶级容器
交互式“抽屉”视图从边缘拉出
窗口。使用android:layout_gravity
控制抽屉定位和布局
子视图的属性对应于您视图的哪一侧
希望抽屉出现:左或右。 (或者开始/结束
支持布局方向的平台版本。)使用
DrawerLayout
,将您的主要内容视图定位为具有宽度的第一个子视图
和match_parent
的高度。在主内容视图后添加抽屉作为子视图并设置
layout_gravity
恰当。抽屉通常使用match_parent
作为具有固定宽度的高度。
DrawerLayout
?,简单来说:
android:layout_gravity="start"
或android:layout_gravity="left"
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close) mDrawerLayout.setDrawerListener(mDrawerToggle);
toggle()
函数的Toolbar.navigationIcon
private void toggle() { if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) { mDrawerLayout.closeDrawer(GravityCompat.START); } else { mDrawerLayout.openDrawer(GravityCompat.START); } }
/** Push object to x-axis position at the start of its container, not changing its size.*/ public static final int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT;
Toggle()
函数调用mDrawerLayout.openDrawer(Gravity.START)
/*** Open the specified drawer by animating it out of view. ** @param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right. * GravityCompat.START or GravityCompat.END may also be used. */ public void openDrawer(@EdgeGravity int gravity) { final View drawerView = findDrawerWithGravity(gravity); if (drawerView == null) { throw new IllegalArgumentException("No drawer view found with gravity " + gravityToString(gravity)); } openDrawer(drawerView); }
layout_gravity="start”
或left
设置DrawerLayout的childView,则会引发No drawer view found with gravity LEFT
。
<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">
<!--This will appear when the drawer is closed (default view)-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</LinearLayout>
<!-- This will appear when the drawer is opened -->
<!-- the property,android:layout_gravity, is used internally to identify imageView as the view to be displayed when the drawer is opened -->
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height=" match_parent"
android:layout_gravity="left"
android:background="@drawable/img_shree_ganesha" />
</android.support.v4.widget.DrawerLayout>
I hope that helps. Happy Coding…
答案 2 :(得分:6)
“Android Drawer”需要是“DrawerLayout”的直接子节点。
在上面的示例中(re:原始问题示例),您只有一个直接子节点位于“DrawerLayout”(“LinearLayout”)下,没有抽屉视图。
将抽屉视图移出LinearLayout并将其放在其后面。
答案 3 :(得分:4)
在我的情况下,因为DrawerLayout attr:tools:openDrawer="start"
我为第二个元素添加了android:layout_gravity="start"
<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"
tools:openDrawer="right"
tools:context=".map.MapFragment">
<include layout="@layout/fragment_map" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@color/white"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/drawer_nav_header"/>
</android.support.v4.widget.DrawerLayout>
答案 4 :(得分:2)
添加
android:layout_gravity =“ start”
到NavigationView
像这样
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="@layout/drawer_menu"
android:layout_gravity="start"
android:foregroundGravity="right"
android:id="@+id/nv">
<include layout="@layout/drawer_menu"/>
</com.google.android.material.navigation.NavigationView>
答案 5 :(得分:1)
您是否使用从右到左(RTL)布局?将剩余重力设置在RTL布局上会引发此异常。可以通过设置重力开始而不是左侧来解决
答案 6 :(得分:1)
您应该在DrawerLayout和NavigationView中使用相同的重力:例如:DrawerLayout标签中的tools:openDrawer =“ right”和NavigationView标签中的android:layout_gravity =“ right”
public function FPNL(){
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$query = $this->db->get('parmaster');
$data['PName']=$query->result_array();
$data['query']=$this->User_model->viewAccMaster();
$data['query1']=$this->User_model->viewAccMaster1();
$first = $this->db->query('SELECT MIN(TAcNo) AS `first` FROM `tipup_payment` ORDER BY TAcNo DESC LIMIT 1')->row_array();
$firstID = $this->db->query("SELECT * FROM `tipup_payment` where TAcNo='$first[first]' ORDER BY TAcNo ASC")->result_array();
$last = $this->db->query('SELECT MAX(TAcNo) AS `last` FROM `tipup_payment` ORDER BY TAcNo DESC LIMIT 1')->row_array
$lastID = $this->db->query("SELECT * FROM `tipup_payment` where TAcNo='$last[last]' ORDER BY TAcNo")->result_array();
if(!empty($id)){
$result = $this->db->query("SELECT * FROM daybook WHERE recno = $id")->row_array();
}else{
$result = "";
}
$allData = $this->db->query("SELECT * FROM daybook ORDER BY recno ASC")->result_array();
//$result = $resultQuery->result_array();
if(!empty($id)){
$data['currentID'] = $id;
}else {
$data['currentID'] = $first['first'];
}
$data['firstID'] = $first;
$data['lastID'] = $last;
$data['result'] = $result;
$data['allData'] = $allData;
$this->load->view('TipUp_Loan/Open_Payment_Entry', $data);
}
答案 7 :(得分:0)
如果你仔细检查,你会发现确实有两个顶级元素:LinearLayout和fragment。
答案 8 :(得分:0)
java.lang.IllegalArgumentException:找不到重力的抽屉视图LEFT
<强>解强> 分配 layout_gravity =&#34; start&#34;或者&#34;左&#34; 属性到您的某个DrawerLayout子视图,如果您的抽屉布局已经有子视图。要么 只需在DrawerLayout视图中创建一个子视图,然后给它一个 layout_gravity =&#34; start&#34;或&#34;左&#34; 属性。 例如
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mDrawer_Layout"
tools:context="com.rad5.matdesign.MainActivity">
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<!-- Notice that here an image view as a child of the Drawer layout was -->
<!-- given a layout_gravity="start" -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation_items" />
</android.support.design.widget.CoordinatorLayout>
答案 9 :(得分:0)
我这样解决了这个问题:
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
android:layoutDirection="rtl"
tools:openDrawer="end"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include layout="@layout/content_main" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layoutDirection="rtl"
android:background="@color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/menu_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
AND JAVA:
if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END);
}
我希望这对某人有帮助!