SlidingMenu:“getActionBar()。setHomeButtonEnabled(true);”不采取

时间:2014-03-30 18:21:47

标签: android android-actionbar target slidingmenu android-homebutton

HomeButton未启用以切换菜单!

它适用于我的 testing-app ,但不适用于我想要的应用。

这里是NavigationDrawerActivity:

public class NavigationDrawerActivity extends Activity{

    protected String[] drawerList;
    protected ListView drawerView;
    protected DrawerLayout drawerLayout;
    protected Context context;

    private ActionBarDrawerToggle mDrawerToggle;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;

        drawerView=(ListView) findViewById(R.id.left_drawer);
        drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);


        //Android doesn't accept the ListView.setAdapter here..!?
        drawerList = getResources().getStringArray(R.array.nav_drawer_items);

        ((ListView) findViewById(R.id.left_drawer)).setAdapter(new ArrayAdapter<String>(this, R.layout.menu_list_item, drawerList));

        drawerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent menu;
                switch (position) {
                case 0:
                    drawerLayout.closeDrawer(drawerView);
                    if(context.getClass().equals(ActivityMain.class)) {
                        Log.i("menu", "same Actvity");
                        break;
                    }
                    menu = new Intent(context, ActivityMain.class);
                    menu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(menu);
                    overridePendingTransition(R.anim.in, R.anim.out);
                    break;
                default:
                    drawerLayout.closeDrawer(drawerView);
                    //new RalaAlertToast(context, "default");
                    break;
                }
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle((Activity) context, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(R.string.app_name);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(R.string.app_name);
            }
        };

        drawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        actionAPI14();
    }

    @TargetApi(14)
    private void actionAPI14() {
        if(Build.VERSION.SDK_INT>=14) {//disabled Target Check
            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);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if(! drawerLayout.isDrawerOpen(drawerView)) {
                finish();
                System.exit(0);
                return true;
            }else{
                drawerLayout.closeDrawer(drawerView);
            }
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if(drawerLayout.isDrawerOpen(drawerView)) {
                drawerLayout.closeDrawer(drawerView);
                return true;
            }
        }
        return false;
    }
}

我读过很多关于目标的论坛和其他网站...
但没有任何帮助 可能的原因是什么?

我也将它发布给其他人,他们一直在寻找一种简单的方法来制作自己的菜单。只是扩展这个Claes。 (我搜索了很长时间,在我的测试应用程序中它没有任何问题!)

1 个答案:

答案 0 :(得分:0)

确保您在活动中调用super.onOptionsItemSelected!