如何实现导航抽屉

时间:2016-06-02 06:34:56

标签: java android navigation-drawer

我正在尝试创建一个应用。我没有得到汉堡按钮只有后退箭头显示在操作栏上。如何在操作栏上显示汉堡按钮显示。使用以下代码 -

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MenuItem;


import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;



public class MainActivity extends FragmentActivity {

ActionBarDrawerToggle mDrawerToggle;
DrawerLayout mDrawer;
TextView messageTextView;
ListView mDrawerListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get references to the TextView and DrawerLayout
    messageTextView = (TextView) findViewById(R.id.messageTextView);
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    // These lines are needed to display the top-left hamburger button
    mDrawer.setDrawerListener(mDrawerToggle);
    getActionBar().setDisplayShowHomeEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Make the hamburger button work
    mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerOpened(View drawerView) {
        }
    };



    // Change the TextView message when ListView item is clicked
    mDrawerListView = (ListView) findViewById(R.id.left_drawer);
    mDrawerListView.setOnItemClickListener(new ListView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           messageTextView.setText("Menu Item at position " + position + " clicked.");;
           mDrawer.closeDrawer(GravityCompat.START);
       }
    });
}

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }


    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@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);
}

}

如何在操作栏上显示汉堡按钮。我只在操作栏上显示后退箭头,请告诉我如何在操作栏上显示汉堡按钮。

由于

error description here

3 个答案:

答案 0 :(得分:0)

更改,

mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerOpened(View drawerView) {
        }
    };

要,

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer,
                R.drawable.ic_drawer, R.string.app_name) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
        };

注意:您需要将Icon保留在可绘制文件夹中。或者您可以使用系统的默认值。

快乐的编码。

答案 1 :(得分:0)

以这种方式初始化你的actionBarDrawerToggle:这应该有效

ActionBarDrawerToggle toggle;
toggle = new ActionBarDrawerToggle(this, mDrawer, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
            }

            @Override
            public void onDrawerOpened(View drawerView) {
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                toggle.syncState();

            }


        };

答案 2 :(得分:0)

您可以根据DrawerToggle的状态处理MainActivity中的Home项目选择。这样您就不必为每个片段添加相同的代码。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Only handle with DrawerToggle if the drawer indicator is enabled.
        if (mDrawerToggle.isDrawerIndicatorEnabled() &&
                mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch (item.getItemId()) {
            // Handle home button in non-drawer mode
            case android.R.id.action_settings:
                onBackPressed();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }