Android 设置导航抽屉列表以打开所有设备屏幕的准确一半屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21038058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:53:09  来源:igfitidea点击:

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

androidscreennavigation-drawermargins

提问by Mayur Raval

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.

我想将所有不同设备的抽屉列表打开到屏幕的一半。我尝试将 layout_margineleft 200dp 或 100dp 的硬编码值添加到抽屉列表中。但它不适用于所有设备,它因设备而异。那么我如何才能保持抽屉列表屏幕的一半。我还尝试了各种函数,如 setLeft(int) 等,但由于我使用的是最低版本 8,其中一些不起作用。所以请帮助我。提前致谢。

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

xml for that:

xml:

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

回答by Gopal Gopi

set the width for ListView dynamically...

动态设置 ListView 的宽度...

    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);

回答by Akhil Jain

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

现在,您需要将宽度除以 2

int newWidth=width/2;

Now set width to ListView like this

现在像这样将宽度设置为 ListView

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

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

这将适用于所有设备,并将在所有设备上提供统一的尺寸。

回答by Shihab Uddin

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.

在这里,我使用右侧导航抽屉,当抽屉向左移动时,我会显示主屏幕的 15%。

  // 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);

回答by gentra

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

使用新的 Android 支持设计库,您可以使用android.support.percent.PercentRelativeLayout轻松完成此操作。这是我的解决方案。

<?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 DrawerLayoutand the content. It's because of the fixed margin of 64 dp that is coded directly to the DrawerLayoutthat wouldn't allow you to use a full widht of the drawer. More about that here

如果你问为什么在和内容中有一个android:layout_marginRight="-64dp"和。这是因为 64 dp 的固定边距直接编码到不允许您使用抽屉的全宽。更多关于这里android:layout_marginRight="-64dp"DrawerLayoutDrawerLayout

回答by Ahmad Moussa

 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;