安卓中的导航抽屉不是全屏显示

6
自从Google引入了导航抽屉组件,我尝试使用这个组件创建一个类似Facebook的菜单。问题在于,视觉效果似乎不同。
谷歌的导航抽屉组件在抽屉打开时保留操作栏,而Facebook的则没有。相反,整个屏幕被推向右侧。
我发现有一些库可以实现这一点,但由于我更喜欢不在项目中包含第三方库,是否有任何方法可以实现这一点?谢谢
基于导航抽屉教程的代码
protected void setupMenu(List<String> list, final ListView menu) {
        Adapter customAdapter = new Adapter(getActionBar().getThemedContext(),
                R.layout.item, list);
        menu.setAdapter(customAdapter);
        menu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int pos, long id) {
                String selected = ((TextView) view.findViewById(R.id.itemTxt))
                        .getText().toString();
                // define pass data
                final Bundle bData = new Bundle();
                bData.putString("itemSelect", selected);

                drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                        FragmentTransaction tx = getSupportFragmentManager()
                                .beginTransaction();
                        tx.replace(R.id.mainContent, Fragment.instantiate(
                                MainActivity.this,
                                "com.example.utilities.ContentPage", bData));
                        tx.commit();
                    }
                });

                drawer.closeDrawer(menu);
            }
        });

    }
6个回答

5

如果您想要一个定制化的导航抽屉,那么创建一个是最好的解决方案。我理解您不想使用第三方库,但这个滑动菜单库可以快速地解决您的问题。


3
希望这对您有所帮助。
  <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        >
        <include
            layout="@layout/nav_header_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            />
    </android.support.design.widget.NavigationView>

在默认代码中删除最后两行。

app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"

2

这是一个对我有效的快速解决方案:

<include
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        **android:layout_width="320dp"**
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/drawer"/>

设置包含布局的宽度。对于不同屏幕尺寸的设备,您可以动态地设置此包含布局的宽度。

祝一切顺利!!!


1
如果您查看DrawerLayout的源代码,您会发现控制最小边距的变量是mMinDrawerMargin。
因此,至少有两种解决方案(技巧): 1. 扩展DrawerLayout并使用反射将此变量设置为0。 从所有构造函数调用此方法。
private void init() {
    try {
        Field declaredField = getClass().getSuperclass().getDeclaredField("mMinDrawerMargin");
        declaredField.setAccessible(true);

        declaredField.setInt(declaredField, 0);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
  1. 不是很棘手

像这样覆盖onMeasure方法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // taken from parents logic
    float density = this.getResources().getDisplayMetrics().density;
    int minMargin = (int) (64.0F * density + 0.5F);

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int newWidth = MeasureSpec.makeMeasureSpec(widthSize + minMargin, widthMode);

    super.onMeasure(newWidth, heightMeasureSpec);
}

我还在这里创建了一个示例项目https://bitbucket.org/wnc_21/fsnavigationdrawer


第一个方法抛出了非法参数异常,第二个方法没有起作用 :( - Adam Varhegyi
请提供堆栈跟踪信息。您尝试从我提供的Bitbucket项目中运行该项目了吗? - wnc_21

0
如果您想要实现类似 Facebook 滑动菜单的效果,那么您需要使用 Android 的 SlidingPaneLayout 而不是 NavigationDrawer。
<android.support.v4.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout android:layout_width="250dp"
        android:layout_height="match_parent"
        android:background="#CC00FF00" />

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="match_parent"
        android:background="#CC0000FF" >
        // add toolbar and other required layouts
    </LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>

将工具栏添加到而非操作栏,并应用无操作栏主题。


0
这个问题是由于边距导致的,所以我们必须重置宽度: 我通过实现DrawerListener并将ListView包装在LinearLayout中来解决这个问题,为其他视图留出List View旁边的空间
这是我的监听器
public class NavigationDrawer implements DrawerLayout.DrawerListener {

    private DrawerLayout mDrawerLayout;
    protected boolean expanded = false;

    NavigationDrawer(Activity activity) {
        this.activity = activity;
    }

    public NavigationDrawer bindDrawerLayout(int id) {
        mDrawerLayout = (DrawerLayout) activity.findViewById(id);
        mDrawerLayout.setDrawerListener(this);
        return this;
    }


    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        if (!expanded) {
            Log.i("margin", "here we are");
            LinearLayout layout = (LinearLayout) findViewById(R.id.left_drawer);
            layout.getLayoutParams().width = getResources().getDisplayMetrics().widthPixels;
            layout.requestLayout();
            expanded = true;
        }
    }

    @Override
    public void onDrawerOpened(View drawerView) {
    }

    @Override
    public void onDrawerClosed(View drawerView) {
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }

}

这是我的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/application_drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <FrameLayout
        android:id="@+id/application_content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:background="#000000"
        android:orientation="vertical">

        <ListView
            android:id="@+id/application_left_drawer"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接