【问题标题】:Android Studio - Drawer Toggle of the Toolbar not Opening the DrawerAndroid Studio - 工具栏的抽屉切换不打开抽屉
【发布时间】:2016-06-20 16:07:17
【问题描述】:

任何人都知道我做错了什么,为什么我的工具栏的抽屉切换按钮没有打开导航抽屉?

我在onCreate 中使用以下代码

        Toolbar toolbar;
        NavigationView mNavigationView;
        DrawerLayout mDrawerLayout;

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /**
         *Setup the DrawerLayout and NavigationView
         */
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.navstuff) ;

        /**
         * Setup click events on the Navigation View Items.
         */

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                ...
                return false;
            }
        });

        /**
         * Setup Drawer Toggle of the Toolbar
         */

        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.app_name);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mDrawerToggle.syncState();

我的activity_main.xml 有以下代码

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

            <RelativeLayout
                android:id="@+id/main_rl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </RelativeLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            app:tabGravity="center"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="5dp"
            app:tabSelectedTextColor="@color/colorAccent" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/tabs_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout">

<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/shitstuff"
    app:itemTextColor="@color/colorPrimaryText"
    app:menu="@menu/drawermenu"
    android:layout_marginTop="?attr/actionBarSize"
    />

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

我想用我的 CollapsingToolbarLayout 添加一个Navigation Drawer。但切换按钮没有打开抽屉。 CollapsingToolbarLayout 有时也不会向上滚动。我正在为我的项目使用minSdkVersion 21

【问题讨论】:

  • 我猜这是因为您的DrawerLayout 实际上并没有出现在屏幕上。它被推出是因为 ViewPager 正在填充父 View。将ViewPager 移动到DrawerLayout 内部,NavigationView 上方。
  • 谢谢迈克。但是切换按钮呢?
  • 我就是这么说的。我认为您的切换似乎不起作用,因为抽屉不在屏幕上。可以拖动打开吗?无论哪种方式,您现在拥有的设置都不是您使用DrawerLayout 的方式。
  • 是的,我可以通过拖动打开抽屉。
  • 好的,那么你的DrawerLayout 覆盖了所有内容,所以你不能点击Toolbar。如果您希望抽屉覆盖所有内容,请将 DrawerLayout 设置为布局的根 View,并将 CoordinatorLayout 作为第一个子元素放入其中。

标签: android android-collapsingtoolbarlayout navigation-drawer android-togglebutton


【解决方案1】:

好的,我最终为 activity_mainapp_barcontent_main 创建了 3 个 xml 文件

app_bar

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark"
        app:contentScrim="?attr/colorPrimaryDark"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            app:tabIndicatorHeight="5dp"
            android:background="@color/material_blue_grey_800"
            app:tabIndicatorColor="@color/colorPrimaryDark"
            app:tabSelectedTextColor="@color/colorPrimaryDark"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

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

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

content_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="MainActivity"
    tools:showIn="@layout/activity_main">

        <android.support.v4.view.ViewPager
            android:id="@+id/tabs_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>

</RelativeLayout>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="top"
        android:background="@color/colorPrimaryDark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:minHeight="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:title="" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout">

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

        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/mystuff"
            app:menu="@menu/drawermenu" />

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

我只是在我的ActivityMain class 中不做任何事情就运行,但它是有效的。我还在努力,希望没有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多