在不使用工具栏的情况下在活动中添加ActionBarDrawerToggle

时间:2016-12-05 05:20:25

标签: android navigation-drawer toolbar

我在android studio中创建了一个NavigationDrawerActivity。但是现在我不需要工具栏。所以我从appa-bar_main.xml删除工具栏:

<?xml version="1.0" encoding="utf-8"?>

<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:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="match_parent"
        android:layout_height="56dp" />

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

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

但我需要在屏幕的top_left上使用切换按钮来显示NavigationDrawer。 因为我删除了工具栏,我不能使用下面的代码:

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

现在,我如何在活动中添加切换按钮?

2 个答案:

答案 0 :(得分:1)

您几乎可以使用任何可点击的组件来控制抽屉,例如按钮。您可以创建一个自定义组件,该组件从Button下载并实现DrawerLayout.DrawerListener接口,例如:

package com.test.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.support.v4.widget.DrawerLayout;
import com.test.MainActivity;

public class CustomDrawerButton extends Button implements DrawerLayout.DrawerListener {

    private DrawerLayout mDrawerLayout;
    private int side = Gravity.LEFT;

    public CustomDrawerButton(Context context) {
        super(context);
    }
    public CustomDrawerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomDrawerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void changeState(){
        if ( mDrawerLayout.isDrawerOpen( side ) ){
            mDrawerLayout.closeDrawer( side );
        }else{
            mDrawerLayout.openDrawer( side );
        }
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        Log.e("BUTTOM DRAWER: ", "onDrawerSlide");
    }
    @Override
    public void onDrawerOpened(View drawerView) {
        Log.e("BUTTOM DRAWER: ", "onDrawerOpened");
        setText("Close\ndrawer");
    }
    @Override
     public void onDrawerClosed(View drawerView) {
        Log.e("BUTTOM DRAWER: ", "onDrawerClosed");
        setText("Open\ndrawer");
    }
    @Override
    public void onDrawerStateChanged(int newState) {
         Log.e("BUTTOM DRAWER: ", "onDrawerStateChanged");
    }

    public DrawerLayout getDrawerLayout() {
        return mDrawerLayout;
    }
     public CustomDrawerButton setDrawerLayout(DrawerLayout mDrawerLayout) {
        this.mDrawerLayout = mDrawerLayout;
        return this;
    }
}

您可以在片段的布局中包含此组件:

<com.test.view.CustomDrawerButton
    android:id="@+id/btnOpenDrawer"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:text="Open\ndrawer"/>

在包含CustomDrawerButton

的片段或活动中
customDrawerButton = (CustomDrawerButton)view.findViewById(R.id.btnOpenDrawer);
customDrawerButton.setDrawerLayout( mDrawer );
customDrawerButton.getDrawerLayout().addDrawerListener( customDrawerButton );
    customDrawerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDrawerButton.changeState();
        }
    });

答案 1 :(得分:0)

尝试从布局app_bar_设置操作栏的高度和宽度= 0dp .. 所以它不会对代码产生影响,但会删除操作栏 像这样

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