【问题标题】:slightly move the home icon in actionbar while sliding menu pops up在弹出滑动菜单时稍微移动操作栏中的主页图标
【发布时间】:2014-03-10 15:31:14
【问题描述】:

我正在阅读google's official doc 关于滑动菜单/导航菜单的信息。

我无法实现的一件事是稍微移动操作栏上的主页图标。请参考以下截图 - 弹出滑动菜单时图标会稍微向左移动。该怎么做?

【问题讨论】:

  • 粘贴您当前的代码

标签: android android-actionbar slidingmenu slidingdrawer


【解决方案1】:

在创建ActionBarDrawerToggle 时定义drawable 后,请确保将以下内容添加到Activity 以产生滑动效果:

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    mDrawerToggle.syncState();
}

【讨论】:

    【解决方案2】:

    使用https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

    您将可绘制对象提供给 ActionBarToggle 实例,它会为您处理一切。

    new ActionBarDrawerToggle(this, mDrawerLayout,
                        R.drawable.ic_drawer, R.string.drawer_open,
                        R.string.drawer_close);
    

    它需要一个用于打开和关闭的字符串,一个用于图标的图像,以及它所附加的 DrawerLayout。

    这是我为我的一个应用程序中的所有活动扩展的一个类。它确保抽屉在我的所有活动中都能正常工作。

    import android.app.ActionBar;
    import android.app.ActionBar.LayoutParams;
    import android.content.res.Configuration;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.os.Bundle;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.widget.DrawerLayout;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    
    
    public class ActionBarActivity extends FragmentActivity {
        private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;
        String title;
        View customNav;
        SessionManager sManager;
    
        public String getActionBarTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
            TextView titleView = (TextView) customNav.findViewById(R.id.title);
            titleView.setText(title);
        }
    
        public void closeDrawer(int gravity) {
            mDrawerLayout.closeDrawer(gravity);
        }
    
        public void openDrawer(int gravity) {
            mDrawerLayout.openDrawer(gravity);
        }
    
        public void onCreate(Bundle savedInstanceState, DrawerLayout layout,
                String title) {
            super.onCreate(savedInstanceState);
            this.title = title;
            sManager = ((UberApplication) getApplication()).getSessionManager();
            mDrawerLayout = layout;
    
            ActionBar actionBar = getActionBar();
    
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            customNav = LayoutInflater.from(this)
                    .inflate(R.layout.action_bar, null);
            actionBar.setDisplayHomeAsUpEnabled(mDrawerLayout != null);
    
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayUseLogoEnabled(false);
            actionBar.setDisplayShowHomeEnabled(false);
    
            actionBar.setHomeButtonEnabled(true);
    
            actionBar.setBackgroundDrawable(new ColorDrawable(Color
                    .parseColor("#5385cc")));
            actionBar.setCustomView(customNav, lp);
            actionBar.setDisplayShowCustomEnabled(true);
    
            if (sManager.getUnreadNotificationCount() > 0) {
                Badge badge = (Badge) customNav.findViewById(R.id.badge_holder);
                badge.setBadgeCount(Integer.toString(sManager
                        .getUnreadNotificationCount()));
                badge.setVisibility(View.VISIBLE);
                badge.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        closeDrawer(Gravity.START);
                    }
                });
            }
    
            TextView titleView = (TextView) customNav.findViewById(R.id.title);
            titleView.setText(title);
            if (mDrawerLayout != null) {
                mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                        R.drawable.ic_drawer, R.string.drawer_open,
                        R.string.drawer_close) {
    
                    public void onDrawerClosed(View view) {
                        super.onDrawerClosed(view);
                        invalidateOptionsMenu();
                    }
    
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                        invalidateOptionsMenu();
                    }
                };
                mDrawerLayout.setDrawerListener(mDrawerToggle);
                if (((UberApplication) getApplication()).isFirstLaunchForVersion()) {
                    mDrawerLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mDrawerLayout.openDrawer(Gravity.START);
                        }
                    }, 600);
                }
            }
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            if (mDrawerLayout != null) {
                boolean drawerOpen = mDrawerLayout.isDrawerOpen(Gravity.START);
            }
            return super.onPrepareOptionsMenu(menu);
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            if (mDrawerLayout != null) {
                mDrawerToggle.syncState();
            }
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if (mDrawerLayout != null) {
                mDrawerToggle.onConfigurationChanged(newConfig);
            }
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerLayout != null) {
                if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
                }
            }
            return super.onOptionsItemSelected(item);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多