I want to open the drawerlist to the half of the screen for all different device. i tried hard coded values for layout_margineleft 200dp or 100dp to the drawerlist. but it doesn't work in all device it different from device to device. so how can i maintain the exactly half of the screen for drawerlist. i also tried various function like setLeft(int) etc.but some of them doesn't work due to i use minimum version 8. So please help me. thanks in advance.
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);
xml for that:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/setting_background" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="@dimen/top_news_linear_margin"
android:background="@color/news_list_divider_color"
android:orientation="vertical"
android:padding="@dimen/top_news_linear_padding" >
</RelativeLayout>
<ListView
android:id="@+id/drawer_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:background="@color/setting_background"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@color/news_list_divider_color"
android:dividerHeight="@dimen/news_list_divider_height"
/>
</android.support.v4.widget.DrawerLayout>
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);
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;
来源:https://stackoverflow.com/questions/21038058/android-set-navigation-drawer-list-to-open-exact-half-of-the-screen-for-all-devi