简体   繁体   English

如何在ActionBar中使Drawer图标和徽标成为两个不同的按钮

[英]How to make Drawer icon and logo two different button in ActionBar

I have a problem where I need to be able to click both the logo and the backarrow+hamburger/cater from my action bar. 我有一个问题,我需要能够从我的操作栏中单击徽标和backarrow + hamburger / cater。 when i don't have anything in my backstack, Hamburger icon is displayed and is opening my left drawer (then via animation it changes to arrow to close the drawer). 当我的后备箱中没有任何东西时,将显示Hamburger图标并打开我的左抽屉(然后通过动画将其更改为箭头以关闭抽屉)。

Displaying the icon next to AppLogo: 在AppLogo旁边显示图标:

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

I have ActionBarDrawerToggle to check if it's left Drawer that is opened (because i have also right drawer that must not interfere with the hamburger icon animations). 我有ActionBarDrawerToggle来检查是否打开了左抽屉(因为我还有右抽屉,它一定不能干扰汉堡包图标动画)。

ActionBarDrawerToggle ActionBarDrawerToggle

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

When navigating bask is possible, the icon changes to the Arrow by disabling the indicator 当可以导航时,通过禁用指示器图标变为箭头

onBackStackChanged() 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);
        }
    }

In OnOptionsItemSelected() i take care that left drawer (with right one closing) OR a navigateBack action is made. OnOptionsItemSelected()我注意左抽屉(右一个关闭)或进行了NavigationBack动作。

OnOptionsItemSelected() 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;

Styles.xml Styles.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>
 ...

This is not my project, so i might not know how exactly everything is configured, and i can't make any major changes to it unless necessary, So i need to add onClick method to the logo and I don't know how . 这不是我的项目,所以我可能不知道所有配置的确切方式,除非必要,否则我无法对其进行任何重大更改,因此我需要在徽标上添加onClick方法,我也不知道如何。 I tried <item name="onClick">customLogoClickMethod</item> , but it didn't help. 我尝试了<item name="onClick">customLogoClickMethod</item> ,但没有帮助。

create the view you want on your actionbar. 在操作栏上创建所需的视图。 set all the listeners on it and than set the view to actionbar by getActionBar().setCustomView(); 设置所有侦听器,然后通过getActionBar()。setCustomView()将视图设置为操作栏。

eg: 例如:

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();

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

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