如何使用sherlockActivity的slidingmenu?

时间:2013-04-22 08:05:04

标签: android actionbarsherlock slidingmenu

我想将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);
}
 }

我能够显示带幻灯片的菜单,但是菜单屏幕占据了整个屏幕而不是局部屏幕。另一个问题是我想要显示菜单上的按钮onclick位于左上角screen.How我能做到这一点。

2 个答案:

答案 0 :(得分:1)

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

enter image description here

代码低于......

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>

答案 1 :(得分:0)

最后,我设法通过以下方式实现。我在Sliding菜单库中所做的更改是SlidingActivity扩展Activity所以我将其更改为SherlockActivity,然后以下列方式使用它很棒。

为此,您需要在SherlockAction-Bar中添加SlidingMenu-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);       
    }
} 

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