Full width Navigation Drawer

后端 未结 14 726
名媛妹妹
名媛妹妹 2020-12-08 03:57

I\'d like to create a full width navigation drawer. Setting layout_width to match_parent on @+id/left_drawer yields in width of about

相关标签:
14条回答
  • 2020-12-08 04:37

    Another possible way to solve the issue without overriding too much:

    public class FullScreenDrawerLayout extends DrawerLayout {
    
    ... //List of constructors calling
    ... //super(...);
    ... //init();
    
    /** Make DrawerLayout to take the whole screen. */
    protected void init() {
        try {
    
            Field field = getClass().getSuperclass().getDeclaredField("mMinDrawerMargin");
            field.setAccessible(true);
            field.set(this, Integer.valueOf(0));
    
        } catch (Exception e) {
            throw new IllegalStateException("android.support.v4.widget.DrawerLayout has changed and you have to fix this class.", e);
        }
    }
    

    }

    If, at some point, support library is updated and mMinDrawerMargin is not there anymore you will get exception and fix problem before you publish your next update.

    I didn't make measurements, but suppose there is not so many reflection to affect performance. Furthermore, it executes only per view creation.

    PS it's strange why DrawerLayout is made so inflexible (I'm about private min margin) at this point...

    0 讨论(0)
  • 2020-12-08 04:39

    A variant on Grogory's solution:

    Instead of subclassing I call the following utility method right after I grab a reference to the drawer layout:

    /**
     * The specs tell that
     * <ol>
     * <li>Navigation Drawer should be at most 5*56dp wide on phones and 5*64dp wide on tablets.</li>
     * <li>Navigation Drawer should have right margin of 56dp on phones and 64dp on tablets.</li>
     * </ol>
     * yet the minimum margin is hardcoded to be 64dp instead of 56dp. This fixes it.
     */
    public static void fixMinDrawerMargin(DrawerLayout drawerLayout) {
      try {
        Field f = DrawerLayout.class.getDeclaredField("mMinDrawerMargin");
        f.setAccessible(true);
        f.set(drawerLayout, 0);
    
        drawerLayout.requestLayout();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    0 讨论(0)
  • 2020-12-08 04:40

    Because all these answers did not work on OS 6.0.1, I'll post here the solution that worked for me in combination with DrawerLayout + NavigationView.

    So all what I do is change the width of the NavigationView programatically:

    mNavigationView = (NavigationView) findViewById(R.id.nv_navigation);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mNavigationView.getLayoutParams();
    params.width = metrics.widthPixels;
    mNavigationView.setLayoutParams(params);
    

    This works for all screen sizes.

    0 讨论(0)
  • 2020-12-08 04:40

    Nipper's FullDrawerLayout Class is just simply awesome.. it's performance is also faster than the default drawer how ever you can;t use it on devices with api that don't have view.getLayoutDirection(); (i'e : Class doesn;t work on all gingerbread devices )

    so what i did was

    replaced all

    view.getLayoutDirection();
    

    with the below code

    GravityCompat.getAbsoluteGravity(gravity,ViewCompat.getLayoutDirection(this));
    

    I have my support library updated to the latest also have extended the fullDrawerlayout to the support navigational drawer. Now it works fine Gingerbread devices as well

    0 讨论(0)
  • 2020-12-08 04:40

    Try out this worked for me :

    <include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"/>
    

    Set width of included layout android:layout_width="320dp". For devices with different screen size you can dynamically set the width of this included layout.

    0 讨论(0)
  • 2020-12-08 04:42

    Google recommends having a maxim width of 320 dip as per the UI guidelines here. Moreover, the width can be set by specified the layout_width of the left_drawer ListView.

    0 讨论(0)
提交回复
热议问题