Full width Navigation Drawer

后端 未结 14 728
名媛妹妹
名媛妹妹 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:45

    Based on the Robert's Answer, you can use the layout_marginLeft=-64dp to solve this problem easily.

    However it doesn't seems to work anymore on Android 5.0 and above. So here's my solution that worked for me.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        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:layout_marginRight="-64dp"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <include
            layout="@layout/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="64dp"/>
    
        <include
            android:id="@+id/left_drawer"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/drawer"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    Basically, Add android:layout_marginRight="-64dp" to the root DrawerLayout so all the layout will go to the right for 64dp.

    Then I add the layout_marginRight=64dp to the content so it goes back to the original position. Then you can have a full drawer there.

    0 讨论(0)
  • 2020-12-08 04:45
    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".UserListActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@drawable/common_gradient"
        android:layoutDirection="rtl"
        android:orientation="vertical">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2">
    
            <TextView
                android:id="@+id/userType_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="نوع المستخدم"
                android:textColor="#000000"
                android:textSize="20sp"
                tools:text="نوع المستخدم" />
    
            <TextView
                android:id="@+id/className_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userType_textView"
                android:layout_centerHorizontal="true"
                android:text="إسم القسم"
                android:textColor="#000000"
                android:textSize="16sp"
                tools:text="إسم القسم" />
    
            <ImageButton
                android:layout_width="30dp"
                android:layout_height="20dp"
                android:layout_alignBottom="@+id/userType_textView"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:background="@android:color/transparent"
                android:contentDescription="@string/desc"
                android:onClick="showMenuAction"
                android:scaleType="fitCenter"
                android:src="@drawable/menu" />
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.8"
    
            android:background="#FAFAFA">
    
            <SearchView
                android:id="@+id/user_searchView"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="#9CC3D7" />
    
            <ListView
                android:id="@+id/users_listView"
                android:layout_width="100dp"
                android:layout_height="100dp"
    
                android:layout_alignParentBottom="true"
                android:layout_below="@+id/user_searchView"
                android:layout_centerHorizontal="true"
                android:divider="#DFDEE1"
                android:dividerHeight="1dp" />
        </RelativeLayout>
    
    </LinearLayout>
    
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/navigationDrawerUser"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:layoutDirection="rtl">
    
    
        <ExpandableListView
            android:id="@+id/menu_listView_user"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#195269"
            android:choiceMode="singleChoice"
            android:divider="#2C637D"
            android:dividerHeight="1dp"
            android:groupIndicator="@null">
    
        </ExpandableListView>
    
    </android.support.v4.widget.DrawerLayout>
    

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

    Yes, you have to extend DrawerLayout and override some methods because MIN_DRAWER_MARGIN is private

    Here is a possible solution:

    public class FullDrawerLayout extends DrawerLayout {
    
        private static final int MIN_DRAWER_MARGIN = 0; // dp
    
        public FullDrawerLayout(Context context) {
            super(context);
        }
    
        public FullDrawerLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    
            if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
                throw new IllegalArgumentException(
                        "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
            }
    
            setMeasuredDimension(widthSize, heightSize);
    
            // Gravity value for each drawer we've seen. Only one of each permitted.
            int foundDrawers = 0;
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
    
                if (child.getVisibility() == GONE) {
                    continue;
                }
    
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    
                if (isContentView(child)) {
                    // Content views get measured at exactly the layout's size.
                    final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
                            widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
                    final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
                            heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
                    child.measure(contentWidthSpec, contentHeightSpec);
                } else if (isDrawerView(child)) {
                    final int childGravity =
                            getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
                    if ((foundDrawers & childGravity) != 0) {
                        throw new IllegalStateException("Child drawer has absolute gravity " +
                                gravityToString(childGravity) + " but this already has a " +
                                "drawer view along that edge");
                    }
                    final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                            MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
                            lp.width);
                    final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                            lp.topMargin + lp.bottomMargin,
                            lp.height);
                    child.measure(drawerWidthSpec, drawerHeightSpec);
                } else {
                    throw new IllegalStateException("Child " + child + " at index " + i +
                            " does not have a valid layout_gravity - must be Gravity.LEFT, " +
                            "Gravity.RIGHT or Gravity.NO_GRAVITY");
                }
            }
        }
    
        boolean isContentView(View child) {
            return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
        }
    
        boolean isDrawerView(View child) {
            final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
            final int absGravity = Gravity.getAbsoluteGravity(gravity,
                    child.getLayoutDirection());
            return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
        }
    
        int getDrawerViewGravity(View drawerView) {
            final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
            return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
        }
    
        static String gravityToString(int gravity) {
            if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
                return "LEFT";
            }
            if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
                return "RIGHT";
            }
            return Integer.toHexString(gravity);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 04:53

    you can by below code

     int width = getResources().getDisplayMetrics().widthPixels/2;
            DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) drawer_Linear_layout.getLayoutParams();
            params.width = width;
            drawer_Linear_layout.setLayoutParams(params);
    
    0 讨论(0)
  • 2020-12-08 04:57

    If you want simpler solution you can just set negative margin

    android:layout_marginLeft="-64dp"
    

    for your left_drawer:

    <include
            android:id="@+id/left_drawer"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/drawer"
            android:layout_marginLeft="-64dp"/>
    
    0 讨论(0)
  • 2020-12-08 04:57
    <?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"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <include
                layout="@layout/app_bar_dashboard"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_marginRight="32dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">
    
            <include layout="@layout/view_navigation_menu" />
    
        </android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    

    That's works perfectly for me. Hope help others.

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