繁体   English   中英

将导航抽屉链接到工具栏按钮

[英]Linking the Navigation Drawer,to Toolbar button

我正在使用Navigation Drawer并且ActionBarDrawerToggle在我通过滑动打开抽屉时是同步的,但是,当我单击Toolbar图标时,它不会打开抽屉。 我的代码有什么问题。

 actionBarDrawerToggle = new ActionBarDrawerToggle(Main2Activity.this, drawerLayout, toolbar, R.string.drawer_open, R.string.close_drawer) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            actionBarDrawerToggle.syncState();
        }
    });

我尝试过的事情是:

actionBar.setDisplayHomeAsUpEnabled(true);
  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
          drawerLayout.openDrawer(Gravity.START);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

这是我使用的布局。 我有NavigationView和抽屉布局。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".Main2Activity">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:theme="@style/ToolbarColoredIcon" />
        <include layout="@layout/content_main2" />
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <LinearLayout
            android:id="@+id/nav_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!--header-->
            <include
                android:id="@+id/header"
                layout="@layout/nav_header"

                />
            <!--items-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="@dimen/dp20">

                <include
                    android:id="@+id/nav_menue_item_home"
                    layout="@layout/nav_menue_item" />

                <include
                    android:id="@+id/nav_menue_item_settings"
                    layout="@layout/nav_menue_item" />

                <include
                    android:id="@+id/nav_menue_item_more_apps"
                    layout="@layout/nav_menue_item" />

                <include
                    android:id="@+id/nav_menue_item_rate_us"
                    layout="@layout/nav_menue_item" />

                <include
                    android:id="@+id/nav_menue_item_privacy_policy"
                    layout="@layout/nav_menue_item" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#e3e4e3"
                android:gravity="bottom"
                android:orientation="vertical">

                <com.google.android.gms.ads.NativeExpressAdView
                    android:id="@+id/adViewNative"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    ads:adSize="@string/native_sm_ad_size"
                    ads:adUnitId="@string/native_sm_ad_unit_id" />

            </LinearLayout>

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

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

所以请问,现在问题在哪里?

感谢所有我已经解决了我的问题。 问题实际上不是在代码中,而是在布局中,我添加了一个 TabHost,它不允许单击工具栏,所以这是实际问题。

创建一个toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    >

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

创建 drawer_menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:checkableBehavior="single"
        android:id="@+id/first_group">

        <item
            android:id="@+id/first_item"
            android:title="First Item"
            android:icon="@drawable/first_item"
            />

    </group>

    <group
        android:checkableBehavior="single"
        android:id="@+id/second_group">
        <item
            android:id="@+id/second_item"
            android:title="Second Item"
            android:icon="@drawable/second_item">

        </item>
    </group>

</menu>

那么你的活动格式应该是这样的:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".MainActivity">

        <!--First child represents elements of activity in drawerLayout-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/relative_layout">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                />

        </RelativeLayout>

        <!--Second child (navigation view) represents elements of navigation drawer in drawerLayout-->

        <android.support.design.widget.NavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/navigation_view"
            android:layout_gravity="start"
            android:background="@color/toolbar"
            app:menu="@menu/drawer_menu">

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

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

对于汉堡图标:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

drawerLayout.addDrawerListener(actionBarDrawerToggle);

然后创建一个方法:

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
}

如果有帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM