制作具有正确重力的导航抽屉

时间:2016-08-08 15:19:05

标签: android android-support-library android-toolbar android-navigation-drawer

我正在尝试使用navigaton bar制作应用程序... 这是我的活动布局:

@Override
public void start(Stage primaryStage) throws Exception {
    Label topLabel = new Label("Top...");
    Label topLabel2 = new Label("Top 2...");

    Region rect = new Region();
    rect.setBackground(new Background(new BackgroundFill(Color.AQUA, CornerRadii.EMPTY, Insets.EMPTY)));

    Label bottomLabel = new Label("Bottom...");

    VBox vBox = new VBox();
    vBox.setFillWidth(true);

    VBox.setVgrow(rect, Priority.ALWAYS);

    vBox.getChildren().addAll(topLabel, topLabel2, rect, bottomLabel);

    Scene scene = new Scene (vBox, 300, 300, Color.BLANCHEDALMOND);
    primaryStage.setScene(scene);
    primaryStage.show();
}

这是协调器布局包含工具栏:          

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="right"
tools:openDrawer="right">

<include
    layout="@layout/app_bar_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_navigation"
    app:menu="@menu/activity_navigation_drawer" />

 </android.support.v4.widget.DrawerLayout>

导航工作正常但是当点击ActionBarDrawerToggle时出现此错误并且应用程序兑现:

  

java.lang.IllegalArgumentException:找不到重力的抽屉视图LEFT

以及切换按钮的操作:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />


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

<include layout="@layout/content_navigation" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|start"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

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

我正在收到此通知:

  

方法调用drawer.setDrawerListener(toggle)可能会产生java.lang.NullPointerExeption

2 个答案:

答案 0 :(得分:1)

根据文件

  

要使用DrawerLayout,请将主要内容视图定位为第一个   宽度和高度为match_parent的子节点。添加抽屉作为孩子   主要内容视图后的视图并设置layout_gravity   适当。抽屉通常使用match_parent作为高度   固定宽度。

使用时请包含以下视图

<include
    layout="@layout/app_bar_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您实际上包含2个观看次数,共有3次观看。
将您的布局封装在LinearLayout中,将2个视图合并到1中。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


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

    <include layout="@layout/content_navigation" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

</LinearLayout>

答案 1 :(得分:1)

我对我的代码进行了更改并且效果很好:

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

    };
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (drawer.isDrawerOpen(GravityCompat.END)) {
                drawer.closeDrawer(GravityCompat.END);
            } else {
                drawer.openDrawer(GravityCompat.END);
            }
        }
    });

而不是:

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();