1

我想SlidingMenuSherlockActivity. 我不知道如何使用它。我能够集成这两个库,即(SlidingMenu and SherlockActionBar)。我以以下方式尝试了它。

public class MainActivity extends SherlockActivity {

private SlidingMenu slidemenu;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            createSlidingMenu();
}

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    // TODO Auto-generated method stub
    menu.add("Search")
    .setIcon(android.R.drawable.ic_search_category_default)
    .setActionView(R.layout.collapsible_edittext)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |  MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}

private void createSlidingMenu() {

    slidemenu = new SlidingMenu(this);
    slidemenu.setShadowDrawable(R.drawable.shadow);
    slidemenu.setShadowWidthRes(R.dimen.shadow_width);
    slidemenu.setFadeDegree(0.35f);
    slidemenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
            slidemenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
    slidemenu.setMenu(R.layout.sliding_view);
    slidemenu.setSlidingEnabled(true);
}
 }

我可以用幻灯片显示菜单,但是该菜单屏幕占据了整个屏幕而不是部分屏幕。另一个问题是我想通过点击屏幕左上角的按钮来显示菜单。如何我可以做到这一点。

4

2 回答 2

1

我已经使用了这个库https://github.com/SimonVT/android-menudrawer/检查该库的示例,我还使用了 actionbarsherlock

在此处输入图像描述

代码如下...

package com.example.slider;

import net.simonvt.menudrawer.MenuDrawer;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockActivity {

    private static final String STATE_MENUDRAWER = "net.simonvt.menudrawer.samples.WindowSample.menuDrawer";
    private static final String STATE_ACTIVE_VIEW_ID = "net.simonvt.menudrawer.samples.WindowSample.activeViewId";

    private MenuDrawer mMenuDrawer;
    private TextView mContentTextView;

    private int mActiveViewId;

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

        setContentView(R.layout.activity_main);

        if (savedInstanceState != null) {
            mActiveViewId = savedInstanceState.getInt(STATE_ACTIVE_VIEW_ID);
        }

        mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_WINDOW);
        // mMenuDrawer.setContentView(R.layout.activity_windowsample);
        mMenuDrawer.setMenuView(R.layout.menu_scrollview);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onRestoreInstanceState(Bundle inState) {
        super.onRestoreInstanceState(inState);
        mMenuDrawer.restoreState(inState.getParcelable(STATE_MENUDRAWER));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(STATE_MENUDRAWER, mMenuDrawer.saveState());
        outState.putInt(STATE_ACTIVE_VIEW_ID, mActiveViewId);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            mMenuDrawer.toggleMenu();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        final int drawerState = mMenuDrawer.getDrawerState();
        if (drawerState == MenuDrawer.STATE_OPEN
                || drawerState == MenuDrawer.STATE_OPENING) {
            mMenuDrawer.closeMenu();
            return;
        }

        super.onBackPressed();
    }

}

不要在您的活动中伪造 android 清单文件中的设置主题

<activity
            android:name="com.example.slider.MainActivity"
            android:theme="@style/Theme.Sherlock.Light"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2013-04-22T09:03:21.037 回答
0

最后我设法以以下方式实现。我在滑动菜单库中所做的更改是SlidingActivity扩展的Activity,所以我将其更改为SherlockActivity然后以下列方式使用,效果很好。

为此,您需要SherlockAction-BarSlidingMenu-library.

public class WhatsUpActivity extends SlidingActivity {

    private SlidingMenu slidingMenu;

    @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whats_up);

    slidingMenu = getSlidingMenu();
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    slidingMenu.setFadeDegree(0.35f);
    setBehindContentView(R.layout.menu_layout); //Menu view left side.

    setSlidingActionBarEnabled(true);       
    }
} 

希望这对将来的某些人有所帮助:)

于 2013-08-14T11:08:06.957 回答