Android将导航抽屉列表设置为打开所有设备屏幕的确切一半
我想为所有不同的设备打开屏幕一半的抽屉列表。我尝试将布局\u margineleft 200dp或100dp的硬编码值写入抽屉列表。但它并不是在所有的设备中都能工作,不同的设备会有所不同。那么,我怎样才能保持抽屉列表屏幕的准确一半呢。我还尝试了各种函数,如setLeft(int)等,但其中一些函数不起作用,因为我使用的是最低版本8。所以请帮帮我。提前谢谢Android将导航抽屉列表设置为打开所有设备屏幕的确切一半,android,screen,navigation-drawer,margins,Android,Screen,Navigation Drawer,Margins,我想为所有不同的设备打开屏幕一半的抽屉列表。我尝试将布局\u margineleft 200dp或100dp的硬编码值写入抽屉列表。但它并不是在所有的设备中都能工作,不同的设备会有所不同。那么,我怎样才能保持抽屉列表屏幕的准确一半呢。我还尝试了各种函数,如setLeft(int)等,但其中一些函数不起作用,因为我使用的是最低版本8。所以请帮帮我。提前谢谢 mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);
用于此的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>
您必须在运行时手动计算屏幕大小并设置抽屉布局的动态宽度,以下是如何在运行时获取设备屏幕宽度和高度 使用此代码可以获得运行时显示的宽度和高度
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
现在,需要将宽度除以2
int newWidth=width/2;
现在像这样设置ListView的宽度
drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));
这将适用于所有设备,并在所有设备上提供统一的大小。动态设置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);
使用新的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>
这是我的解决方案,适用于需要几个百分比才能显示导航抽屉的其他人
在这里,我使用右导航抽屉&当抽屉向左移动时,我显示主屏幕的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);
上述解决方案对我有效,但我从您的回答中了解了确切的操作方法。如果您的listView嵌入到另一个布局中,而该布局是抽屉布局的子布局,我发现您在此包装布局上设置了新的布局参数。按照这个答案所说的去做,但是如果必要的话可以使用listView的父容器。我在这里得到类强制转换异常。我如何继续?适用于所有版本的Android:)这实际上是一个正确的答案。不是因为PercentRelativeLayout,50%可以通过不同的同样正确的方式获得,而是因为-64dp语句,因为这就是它导致内容移动的原因。但是,必须在内容区域中更正-64dp。否则,应用程序内容将向右移动。
// 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);