我正在开发一个 android 项目,我正在尝试使用http://developer.android.com/training/implementing-navigation/nav-drawer.html中的示例集成新的 Navigation Drawer 。
它主要工作除了一件事,操作栏向上按钮不显示菜单,但如果我从边缘滑动手指在主活动屏幕上,然后菜单出现,所以我知道实际菜单没有问题,它只是操作栏按钮。
下面是代码
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
MenuItemAdapter menuAdapter = new MenuItemAdapter(this);
menuAdapter.add(new MenuItem("Hello"));
menuAdapter.add(new MenuItem("World"));
menuAdapter.add(new MenuItem("Parsnips"));
menuAdapter.add(new MenuItem("Turnips"));
mDrawerList.setAdapter(menuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_closed)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARGS_PLANET_NUMBER, position);
fragment.setArguments(args);
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class PlanetFragment extends android.app.Fragment
{
public static final String ARGS_PLANET_NUMBER = "planet_number";
public PlanetFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflator.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARGS_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
//((TextView)rootView.findViewById(R.id.fragment_text_view)).setText(planet);
getActivity().setTitle("Planet: " + planet);
return rootView;
}
}
private class MenuItem
{
public String menuName;
public MenuItem(String menuName)
{
this.menuName = menuName;
}
}
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
public MenuItemAdapter (Context context)
{
super(context,0);
}
public View getView (int position, View convertView, ViewGroup parent)
{
convertView = null;
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_menu_item, null);
}
/*TextView title = (TextView)convertView.findViewById(R.id.row_title);
title.setText(getItem(position).menuName);
title.setVisibility(View.VISIBLE);*/
EditText title = (EditText)convertView.findViewById(R.id.row_edittext);
title.setHint(getItem(position).menuName);
title.setVisibility(View.VISIBLE);
return convertView;
}
}
}
感谢您的任何帮助,您可以提供。