web-dev-qa-db-ja.com

Android DrawerLayout-重力によるドロワービューが見つかりません

引き出しのトグルをクリックすると、次の例外が発生します。

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

ドキュメントから

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


OK。ここで、物事を簡単かつ明確にします。
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によってサポートされるレイアウト。これにより、引き出しと呼ばれるオーバーレイ画面をメイン画面に配置できます。
  • DrawerLayoutは、LinearLayoutおよびRelativeLayoutと同様に、子ビューを機能させる必要があります。
  • 子ビューには、いずれかのプロパティセットが必要です
    _Android:layout_gravity="start"_ OR _Android:layout_gravity="left"_
  • DrawerLayoutはこのchildViewを使用して知る 引き出しの表示を開始する場所 すなわち、左から右または右から左へ。テキストが右から左に読まれる国があります。


技術スタッフ:
  • EVENT#1:ActionBarDrawerToggle.JavaはDrawerLayoutのドロワーリスナーとして設定されます
  •  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); 
     
  • EVENT#2:ユーザーはtoggle()関数を呼び出すToolbar.navigationIconをクリックします
    ActionBarDrawerToggle.Java(Android-sdk\sources\Android-22\Android\support\v7\app\ActionBarDrawerToggle.Java)
     private void toggle(){
     if(mDrawerLayout.isDrawerVisible(GravityCompat.START)){
     mDrawerLayout.closeDrawer(GravityCompat.START); 
    } else {
     mDrawerLayout.openDrawer(GravityCompat.START); 
    } 
    } 
     

    Gravity.Java(Android-sdk\sources\Android-22\Android\view\Gravity.Java)
    / **コンテナの開始時にオブジェクトをx軸の位置にプッシュします。
    サイズを変更しません。*/
     public static final int START = RELATIVE_LAYOUT_DIRECTION | Gravity.LEFT; 
     
  • EVENT#3:Toggle()関数はmDrawerLayout.openDrawer(Gravity.START)を呼び出します
    DrawerLayout.Java (support-v4-22.1.1.jarライブラリー)
    /***指定された引き出しをアニメートして開きます。 ** 
     @param gravity Gravity.LEFTで左の引き出しを移動するか、Gravity.RIGHT 
    で右に移動します。 * GravityCompat.STARTまたはGravityCompat.ENDも使用できます
    。 */
     public void openDrawer(@EdgeGravity int gravity){
     final Viewドロワービュー= findDrawerWithGravity(gravity); 
     if(drawerView == null){
     throw new IllegalArgumentException( "重力で引出しビューが見つかりませんでした" + 
     gravityToString(gravity)); 
    } 
     openDrawer(drawerView); 
    } 
     
  • EVENT#4:DrawerLayoutのchildViewが_layout_gravity="start”_またはleftで設定されていない場合、_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…
_
14
Devendra Vaja

「Android Drawer」は、「DrawerLayout」の直接の子ノードである必要があります。

上記の例(元の質問の例)では、「DrawerLayout」(「LinearLayout」)の下に直接の子ノードが1つだけあり、その後に引き出しビューなしがあります。

引き出しビューをLinearLayoutから移動して、その後ろに配置します。

7
WindSpirit

私の場合、DrawerLayout attr:tools:openDrawer="start" 追加した Android:layout_gravity="start" 2番目の要素

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

DrawerLayoutとNavigationViewで同じ重力を使用する必要があります。例:DrawerLayoutタグのtools:openDrawer = "right"とNavigationViewタグのAndroid:layout_gravity = "right"

<?xml version="1.0" encoding="utf-8"?>
<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>
1
Maryam Azhdari

右から左(RTL)レイアウトを使用していますか? RTLレイアウトに重力を設定すると、この例外がスローされます。これは、左ではなく重力開始を設定することで修正できます

1
user1474089

より詳細に確認すると、実際には2つの最上位レベルの要素、LinearLayoutとfragmentがあることに気付くでしょう。

0
asiby

Java.lang.IllegalArgumentException:重力LEFTでドロワービューが見つかりません

[〜#〜] solution [〜#〜]割り当てlayout_gravity = "start"または "left"ドロワーレイアウトが既にある場合、DrawerLayout子ビューのいずれかに属性を割り当てます子ビュー。 OR DrawerLayoutビュー内に子ビューを作成し、それにlayout_gravity = "start"または "left"属性を指定するだけです。たとえば、

 <?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>
0
Peter Akwa