Android的DrawerLayout - 没有重力发现抽屉查看重力、抽屉、发现、Android

2023-09-05 00:25:06 作者:掉了颗兔牙

当我点击我的抽屉里拨动我我得到下面的异常

  

java.lang.IllegalArgumentException:如果没有重力发现抽屉查看   左

这是我的activity_drawer.xml

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.widget.DrawerLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真正的>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

    < android.support.v7.widget.Toolbar
        机器人:ID =@ + ID /工具栏
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent
        机器人:=了minHeight?ATTR / actionBarSize
        机器人:后台= />中ATTR / colorPrimary?

    <的FrameLayout
        机器人:ID =@ + ID /内容
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

    <片段
        机器人:ID =@ + ID /导航
        机器人:layout_width =240dp
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        机器人:名称=com.xyz.ui.navigation.NavigationFragment
        工具:布局=@布局/ fragment_navigation/>

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

我fragment_navigation.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的ListView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_gravity =开始>

< / ListView控件>
 
AndroidUI 侧滑菜单 DrawerLayout的使用

和我的项目

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:文本=大文本
        机器人:ID =@ + ID / navigation_item_text
        机器人:layout_gravity =center_horizo​​ntal/>
< / LinearLayout中>
 

解决方案

从技术文档

  

要使用DrawerLayout,您的主要内容视图作为第一个孩子的宽度和match_parent的高度位置。添加抽屉为孩子的意见主要内容视图后,并设置适当的layout_gravity。抽屉通常使用match_parent的高度与一个固定的宽度。

 < android.support.v4.widget.DrawerLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真正的>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

    < android.support.v7.widget.Toolbar
        机器人:ID =@ + ID /工具栏
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent
        机器人:=了minHeight?ATTR / actionBarSize
        机器人:后台= />中ATTR / colorPrimary?

    <的FrameLayout
        机器人:ID =@ + ID /内容
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

    < / LinearLayout中>

    <片段
        机器人:ID =@ + ID /导航
        机器人:layout_width =240dp
        机器人:layout_height =match_parent
        机器人:layout_gravity =右
        机器人:名称=com.xyz.ui.navigation.NavigationFragment
        工具:布局=@布局/ fragment_navigation/>

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

When i'm click on my drawer toggle i'm get the following exception

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

This is my 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>

My 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>

And my list item

<?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>

解决方案

From documentation

To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

<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>