【问题标题】:Cannot see ActionBar toggle button看不到 ActionBar 切换按钮
【发布时间】:2015-05-05 11:20:36
【问题描述】:

即使在编码之后,我也无法在预览中看到切换按钮。我错过了什么吗?

我想在那个切换按钮上实现导航抽屉,它会从右到左打开,再次点击它会关闭。

下面是我的代码:

public class entryActivity extends ActionBarActivity {

    private String[] mMenuItems;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry);

        //https://developer.android.com/training/implementing-navigation/nav-drawer.html
        mMenuItems = getResources().getStringArray(R.array.menu_items);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mMenuItems));
        ActionBar actionBar = getSupportActionBar();

        actionBar.setHomeButtonEnabled(true);


        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.string.open,
                R.string.close
        ){
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if(actionBarDrawerToggle.onOptionsItemSelected(item)){
            return true;
        }

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

【问题讨论】:

    标签: android android-actionbar navigation-drawer android-togglebutton


    【解决方案1】:

    在 onCreate 方法中设置。

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    

    【讨论】:

    • 显示后退图标按钮不切换一个,点击该按钮,应用程序崩溃..
    【解决方案2】:

    尝试添加setDisplayHomeAsUpEnabled(),就像你添加setHomeButtonEnabled()一样

    对于汉堡菜单选项:http://codetheory.in/android-navigation-drawer/

    【讨论】:

    • 它显示了带有后退符号的按钮,但我希​​望它是汉堡菜单.. 按下按钮时,应用程序崩溃了。
    【解决方案3】:

    您需要拨打actionBarDrawerToggle.syncState():

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }
    

    查看完整示例:https://developer.android.com/training/implementing-navigation/nav-drawer.html

    【讨论】:

    • 它确实有效,但是当抽屉拉出时,应用程序停止工作。
    • 有什么例外?你在使用支持库吗?尝试将invalidateOptionsMenu() 更改为supportInvalidateOptionsMenu()
    • 不知道异常,但我的目标是 API 16 以上,所以它不应该导致支持库问题。我尝试使用更新的代码,应用程序仍然崩溃。
    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2011-07-28
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多