将 200dp 或 100dp layout_margineleft硬编码值到抽屉列表。 但它并非在所有设备上都有效,它因设备而异。 那么我怎样才能保持抽屉列表屏幕的正好一半。 我还尝试了各种功能,如setLeft(int)等,但由于我使用最小值,其中一些不起作用版本 8。所以请帮助我。提前谢谢。
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>
动态设置列表视图的宽度...
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);
您必须在运行时手动计算屏幕尺寸并将动态宽度设置为抽屉布局,这是在运行时获取设备屏幕宽度和高度的方法
使用此代码,您可以获取运行时显示器的宽度和高度
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
现在,您需要将宽度除以2
int newWidth=width/2;
现在像这样将宽度设置为列表视图
drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));
这将适用于所有设备,并将为所有设备提供统一的尺寸。
这是我为需要几个百分比来显示导航抽屉的其他人提供的解决方案。
在这里,我使用右侧导航抽屉,当抽屉向左移动时,我显示主屏幕的 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);
使用新的 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>
如果你问为什么DrawerLayout
和内容中有android:layout_marginRight="-64dp"
和android:layout_marginRight="-64dp"
。这是因为 64 dp 的固定边距直接编码到DrawerLayout
,不允许您使用抽屉的完整宽度。更多关于这里的信息
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;