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

May*_*val 26 android screen margins navigation-drawer

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

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Gop*_*opi 70

动态设置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);
Run Code Online (Sandbox Code Playgroud)

  • 如果您的listView嵌入在DrawerLayout的子窗体的另一个布局中,我发现您在此包装布局上设置了新的布局参数.做这个答案的所有内容,但必要时使用listView的父容器. (4认同)
  • 我在这里得到类强制转换异常..我该如何继续? (2认同)

Akh*_*ain 7

您必须手动计算屏幕大小并在运行时将动态宽度设置为抽屉布局,以下是如何在运行时获取设备屏幕宽度和高度

使用此代码,您可以获得运行时显示的宽度和高度

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)

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

int newWidth=width/2;
Run Code Online (Sandbox Code Playgroud)

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

drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));
Run Code Online (Sandbox Code Playgroud)

这将适用于所有设备,并将在所有设备上提供统一的大小.


Shi*_*din 5

这是我为其他需要几个百分比来显示导航抽屉的人提供的解决方案。

在这里,我使用右侧导航抽屉,当抽屉向左移动时,我显示主屏幕的 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);
Run Code Online (Sandbox Code Playgroud)