【问题标题】:Navigation Drawer toggle button not working导航抽屉切换按钮不起作用
【发布时间】:2017-09-14 10:18:10
【问题描述】:

在搜索this 之类的类似问题但无法获得结果后
我已经在我的活动中定义了一个切换按钮,例如

toolbar = (Toolbar) findViewById(R.id.toolbar);
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.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);

这是我定义工具栏的xml

<android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <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" >

                <SearchView
                    android:id="@+id/searchView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:iconifiedByDefault="false"
                    android:queryHint="Search"
                    android:layout_centerHorizontal="true"
                    android:focusable="false"/>

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"
                app:tabGravity="fill"/>
        </android.support.design.widget.AppBarLayout>

这是我在此之后能够实现的目标

如您所见,我无法在工具栏上获得导航切换按钮
任何帮助将不胜感激!

【问题讨论】:

  • 完整的 xml pastebin.com/57zUmnhd
  • 你的抽屉布局在哪里
  • @BhuvaneshBs 检查上面的评论我已经给出了完整的 xml

标签: android navigation android-toolbar navigation-drawer


【解决方案1】:

尝试在定义drawer = (DrawerLayout) findViewById(R.id.drawer_layout);之前输入setSupportActionBar(toolbar);

编辑:

在我检查了你的 app_main_layout.xml 之后,还有一个

<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>

在那里,我认为这会让人感到困惑,因为它在你的 activity.xml

中也有一个相同的 ID,即 @+id/toolbar

您必须更改 id,或将其删除,然后解决。

【讨论】:

  • 不工作它是最初的实现,之后我把setSupportActionBar(toolbar);放在底部
  • 等等,让我试试你的xml,我认为问题出在那儿
  • 你能分享你的内心吗@layout/app_bar_main ?
  • 这就是问题所在,您必须删除 app_bar_main 中的 appbarlayout
  • 在移除 app_bar_main 中的 AppBarLayout 和 Toolbar 后,它可以在我的设备上完美运行
【解决方案2】:

您必须先setSupportActionBar

你的实现应该是这样的:

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

  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.addDrawerListener(toggle);
  toggle.syncState();

【讨论】:

  • 不工作它是最初的实现,之后我把setSupportActionBar(toolbar);放在底部
  • 再试一次。复制并使用上面肯定可以工作的代码
  • 做了同样的结果:(
  • 你的抽屉布局在哪里?
【解决方案3】:

添加以下代码;

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2021-11-07
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    相关资源
    最近更新 更多