3

我已经在我的应用程序中调整了带有图标的导航抽屉,如图1所示,看起来还可以。我想像谷歌地图一样在菜单的右下角显示图标。

图1 图2

代码:

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

private void setupSlideMenu() {
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
    addIcon();
    mDrawerList = (ListView) findViewById(R.id.left_drawer1);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanetTitles));
    // Set the list's click listener
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    log("setupSlideMenu", false);
}

private void addIcon() {
    mDrawerToggle = new ActionBarDrawerToggle(
    this,                   /* host Activity */
    mDrawerLayout,          /* DrawerLayout object */
    R.drawable.ic_drawer,   /* nav drawer icon to replace 'Up' caret */
    R.string.drawer_open,   /* "open drawer" description */
    R.string.drawer_close   /* "close drawer" description */
    ) {
        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };
    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}
@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);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...
    return super.onOptionsItemSelected(item);
}

任何想法?提前致谢!

4

1 回答 1

2

使用BottomDrawerToggle。解决方案非常简单:

  1. 添加ImageView到带有重力 BOTTOM 的 android.R.id.content FrameLayout

  2. ImageView通过将左右边距设置为可绘制宽度一半的负值,使一半可见。

  3. 解析内部资源action_bar_icon_vertical_padding并将其用作图像视图的底部填充

    int id = Resources.getSystem().getIdentifier("action_bar_icon_vertical_padding", "dimen", "android");
    float padding = mActivity.getResources().getDimension(id);
    
  4. 创建SlidingDrawable并计算了如何在抽屉滑入/滑出draw(Canvas canvas)方法时动态隐藏可绘制对象。Drawable 通过裁剪Rect抽屉的可见像素数量来隐藏。

    public void draw(Canvas canvas)
    {
      float pixelsVisible = mOffset * mMinusShadow;
      int target = (int) ((mWrapped.getIntrinsicWidth()/2)+pixelsVisible);
      if(mGravity == GravityCompat.START || mGravity == Gravity.LEFT)
          canvas.clipRect(target, 0, mWrapped.getIntrinsicWidth(), mWrapped.getIntrinsicHeight());
      else
          canvas.clipRect(0, 0, mWrapped.getIntrinsicWidth() - target,mWrapped.getIntrinsicHeight());       
      this.mWrapped.draw(canvas);
      canvas.restore();
    }
    
于 2013-10-13T19:39:15.743 回答