在包含导航抽屉的应用程序中对ActionBar进行基本自定义

时间:2014-09-03 13:12:03

标签: android android-actionbar android-navigation

我正在尝试在我的应用程序中使用导航抽屉和操作栏。

操作栏中最左上角的按钮可打开和关闭抽屉,并且在抽屉关闭状态下没有标题。但是当抽屉打开时,标题会重新出现。标题是项目名称。我想从每个场景中删除这个标题。在下面的代码中,您可以看到我已经尝试在抽屉打开和关闭内再次设置它

private NavigationDrawerFragment mNavigationDrawerFragment;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

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

    setTitle("");
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setIcon(R.drawable.plus);

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_closed)
    {
        /*
         * Called when a drawer has settled in a completely closed state
         */
        public void onDrawerClosed(View view)
        {
            Log.d("drawerToggle", "Drawer closed");
            super.onDrawerClosed(view);
            setTitle("");
            //getActionBar().setTitle(R.string.app_name);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
        }

        /*
         * Called when a drawer has settled in a completely open state
         */
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

当抽屉打开/关闭时,三条水平线会发生变化,完全移除这些线条的最佳方法是什么(但保留主图标)?目前我只是将ic_drawer设置为透明图像。

以下是我的代码,当抽屉打开/关闭时,它会更改菜单。它似乎有用,但这是最好的方法吗?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        inflater.inflate(R.menu.main, menu);
        //restoreActionBar();
        System.out.println("inhere");
        //return true;
        //return super.onCreateOptionsMenu(menu);
    }
    else
        inflater.inflate(R.menu.close_left_drawer, menu);
    return super.onCreateOptionsMenu(menu);
}

感谢。

1 个答案:

答案 0 :(得分:0)

setTitle将设置标题,setIcon会将图标设置为ActionBar

getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);

参考:Adding Up Actionhttp://developer.android.com/guide/topics/ui/actionbar.html