我正在開發一個安卓應用程序Sliding Menu和ActionBar Sherlock和我迷路了。我無法打開滑動菜單
我跟着這Spanish tutorial將它們添加到我的項目。
這是我的代碼:
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
public class MainActivity extends SlidingFragmentActivity
{
/**
* Used as TAG on Log messages.
*/
public static final String TAG = "MainActivity";
private SlidingMenu menu;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Generate keys to encrypt and decrypt user password.
generateCryptoKeys();
setBehindContentView(R.layout.menu);
setSlidingActionBarEnabled(false);
menu = new SlidingMenu(this);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
這是菜單片段:
public class MenuFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.menu_list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
}
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
我有幾個問題:
1.我必須使用片段?
2.爲什麼我點擊主頁按鈕後無法打開菜單?看到如下圖所示:
要操作欄福爾摩斯與滑動菜單集成我已經做到了這一點:
Go into the SlidingActivities that you plan on using make them extend Sherlock___Activity instead of ___Activity.
而且,當我做到這一點:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
我得到這個錯誤:
Cannot override the final method from SherlockFragmentActivity
。
謝謝你的回答,但是:我需要做些什麼才能打開菜單? – VansFannel
如果我這樣做:'public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)',我可以打開滑動菜單。 – VansFannel