Android set navigation drawer list to open exact half of the screen for all device screen

五迷三道 提交于 2019-12-02 20:15:47

set the width for ListView dynamically...

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);

    int width = getResources().getDisplayMetrics().widthPixels/2;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
    params.width = width;
    mDrawerList.setLayoutParams(params);

You have to manual calculate the screen size and set dynamic width to the drawer layout at runtime, here is how to get device Screen Width and Height at run time

Using this code you can get runtime Display's Width & Height

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

Now, you need to divide the width by 2

int newWidth=width/2;

Now set width to ListView like this

drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));

This will work for all the devices and will give uniform size across all.

here is my solution for others who need several percentage to show there navigation drawer.

Here, I use right navigation drawer & i show 15% of home screen when drawer move left.

  // convert your offset percent (here 15% ) into decimal by dividing 100 
    float offset = .15f * getResources().getDisplayMetrics().widthPixels ;
    float width = getResources().getDisplayMetrics().widthPixels - offset;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) rightNavigationView.getLayoutParams();
    params.width = (int) width;
    rightNavigationView.setLayoutParams(params);
gentra

Using the new Android Support Design Library, you can make use of the android.support.percent.PercentRelativeLayout to accomplish this easily. Here's my solution.

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

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        >

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:fitsSystemWindows="true"
            app:layout_widthPercent="50%"
            app:headerLayout="@layout/nav_header_dashboard"
            app:menu="@menu/menu_drawer_activity_dashboard"/>

    </android.support.percent.PercentRelativeLayout>

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

If you're asking why there's a android:layout_marginRight="-64dp" and android:layout_marginRight="-64dp" in the DrawerLayout and the content. It's because of the fixed margin of 64 dp that is coded directly to the DrawerLayout that wouldn't allow you to use a full widht of the drawer. More about that here

 int currentVerion = Build.VERSION.SDK_INT;
 int width= 0;
 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();

    if(currentVerion >= Build.VERSION_CODES.BASE) {
        display.getSize(size);
        width = size.x;
    }
    else
   {
       width = display.getWidth();
   }

((ListView) view.findViewById(R.id.top_sectionlist)).getLayoutParams().width =width/2;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!