Is it possible to use something other than a listview as sliding drawer in drawerlayout

后端 未结 4 1587
陌清茗
陌清茗 2020-12-13 00:36

I would like to have for example a LinearLayout or a RelativeLayout sliding from the left of the screen instead of a lone ListView.

4条回答
  •  北海茫月
    2020-12-13 01:09

    This will work if you move both the android:id="@+id/left_drawer" (or create a new id) and set the gravity.

    The id move (or new one) is so the reference is correct so you call closeDrawer() on it and not the child views.

    But most importantly, the DrawerLayout requires that element to have a android:layout_gravity set on it, as you mentioned.

    Finally, you need to call close closeDrawer() on the base view, the one with the required gravity.

    Example:

    
    
        
    
        
    
            
    
            
    
        
    
    

    In code:

    DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout)
    LinearLayout mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
    ListView mDrawerListChild = (ListView) findViewById(R.id.left_drawer_child);
    
    ...
    
    mDrawer.closeDrawer(mDrawerLinear);
    

    (This is basically what @Karakuri posted, but with a more complete explanation and example.)

提交回复
热议问题