【问题标题】:Navigation Drawer icon doesn't replace up caret导航抽屉图标不会替换插入符号
【发布时间】:2018-11-08 19:11:18
【问题描述】:

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

  • 我想要什么 - 使用自定义抽屉图标而不是向上插入符号。
  • 什么问题- 自定义图标不显示。它是始终可见的“向上”插入符号。
  • 我做了什么-

  • 使用 Android studio 创建 NavDrawer 项目。

  • 在 Stackoverflow 上搜索了类似的问题。我有 onPostCreate() 方法的参考资料,但我已经处理好了。

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

  • 查看图片- 查看左上角。向上插入符号是我想用我的图标替换的内容

主活动

包 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);
        }
    }

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    在您的主要活动中,在 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);
    

    【讨论】:

    • 嗨 Ivano - 这意味着 ActionBarDrawerToggle 中的可绘制对象没有设置主页图标。正确的 ?我的系统正在升级..我会在升级完成后尽快解决。
    • 我也遇到了同样的问题,我就这样解决了。我不知道为什么它在 ActionBarDrawerToggle 中没有改变
    • 非常感谢。这将是我正在阅读的第 1001 个 SO 线程。但我终于松了一口气。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多