0

我有一个问题,我需要能够从我的操作栏中单击徽标和后退箭头+汉堡包/餐饮。当我的 backstack 中没有任何东西时,会显示汉堡图标并打开我的左抽屉(然后通过动画它变为箭头以关闭抽屉)。

在 AppLogo 旁边显示图标:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

我必须ActionBarDrawerToggle检查它是否打开了左抽屉(因为我也有右抽屉,不能干扰汉堡图标动画)。

ActionBarDrawer 切换

 mDrawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_closed) {

        //must override these methods to make only left drawer change the icon of drawer in the corner, when opened

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerSlide(drawerView, slideOffset);
            }
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerOpened(drawerView);
            }
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerClosed(drawerView);
            }
        }
    };
    drawerLayout.setDrawerListener(mDrawerListener);

当可以导航时,图标会通过禁用指示器变为箭头

onBackStackChanged()

@Override
    public void onBackStackChanged() {
        // disable drawer icon and leave backarrow icon if backstack not empty
        boolean canBack = getSupportFragmentManager().getBackStackEntryCount() > 0;
        if (canBack) {
            mDrawerListener.setDrawerIndicatorEnabled(false);
        } else {
            mDrawerListener.setDrawerIndicatorEnabled(true);
        }
    }

OnOptionsItemSelected()我照顾左抽屉(右一个关闭)或navigateBack动作。

OnOptionsItemSelected()

public boolean onOptionsItemSelected(MenuItem item) {
    Helper.dismissKeyboard(this);
    switch (item.getItemId()) {
        case android.R.id.home:
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                onSupportNavigateUp();
            } else {
                if (drawerLayout.isDrawerOpen(drawerSports)) {
                    drawerLayout.closeDrawer(drawerSports);
                } else {
                    if (drawerLayout.isDrawerOpen(lvMyAccount)) {
                        drawerLayout.closeDrawer(lvMyAccount);
                    }
                    drawerLayout.openDrawer(drawerSports);
                }

            }
            return true;

样式.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" >
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
    <item name="android:logo">@drawable/logo_marathon</item>
    <item name="logo">@drawable/logo_marathon</item>
    <item name="android:displayOptions">showHome|useLogo</item>
    <item name="displayOptions">showHome|useLogo</item>

</style>
 ...

这不是我的项目,所以我可能不知道一切是如何配置的,除非必要,否则我无法对其进行任何重大更改,因此我需要将 onClick 方法添加到徽标中,我不知道如何。我试过 <item name="onClick">customLogoClickMethod</item>了,但没有帮助。

4

1 回答 1

0

在操作栏上创建所需的视图。在其上设置所有侦听器,然后通过 getActionBar().setCustomView() 将视图设置为操作栏;

例如:

getActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO
                        | ActionBar.DISPLAY_SHOW_HOME
                        | ActionBar.DISPLAY_HOME_AS_UP);

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // inflate the view that we created before
        View v = inflater.inflate(R.layout.sample_titlebar, null);

//set all the listeners here with that view 
//and than

getActionBar().setCustomView();
于 2015-05-15T07:13:30.303 回答