导航抽屉图标不会替换插入符号

时间:2015-07-16 12:55:14

标签: android navigation-drawer

我正在尝试使用自定义抽屉图标实现一个简单的导航抽屉。

  • 我想要什么 - 使用自定义抽屉图标代替向上插入符号。
  • 有什么问题 - 自定义图标无法显示。这是' Up'插入符号,一直可见。
  • 我做了什么 -

  • 使用Android工作室创建NavDrawer项目。

  • 在Stackoverflow上搜索类似的probs。我已经获得了onPostCreate()方法的参考资料,但我已经处理过了。

  • 使用Android示例应用运行该项目。

  • 查看图片 - 请参见左上角。我希望用我的图标

  • 替换up caret

MainActivity

package com.deep.wealthtrackernew;

    import android.content.res.Configuration;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ListView;

    import java.util.List;


    public class MainActivity extends ActionBarActivity {

        private ListView listView;
        private DrawerLayout drawerLayout;
        private ActionBarDrawerToggle actionBarDrawerToggle;

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

            listView = (ListView) findViewById(R.id.left_drawer);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer,
                    R.string.open_drawer, R.string.close_drawer) {

                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
    //                getActionBar().setTitle();
                    supportInvalidateOptionsMenu();
                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                    getSupportActionBar().setTitle("Wealth Tracker");
                    supportInvalidateOptionsMenu();
                }
            };

            drawerLayout.setDrawerListener(actionBarDrawerToggle);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            actionBarDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            actionBarDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // If the nav drawer is open, hide action items related to the content view
            boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
    //        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @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.

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

            int id = item.getItemId();

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

            return super.onOptionsItemSelected(item);
        }
    }

See the top left corner. The up caret is what I would like to replace with my icon

2 个答案:

答案 0 :(得分:4)

在setContentView执行此操作之后,在主活动中创建

  //if you use support action bar it is
 ActionBar actionBar=getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.newIcon);
 //if you are not using support action bar it is
ActionBar actionBar=getActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.newIcon);

答案 1 :(得分:1)

这解决了我的问题(因为我有一个BaseActivity,并在MainActivity中声明了以下内容,它扩展了BaseActivity)。在 onCreate(Bundle savedInstanceState)方法内设置应用程序图标,如下所示:

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);