Android 导航抽屉图标 (ic_drawer) 未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20385101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:46:52  来源:igfitidea点击:

Navigation Drawer Icon (ic_drawer) not showing

androidiconsandroid-actionbarnavigation-drawer

提问by Adifyr

I am implementing a navigation drawer for my app. Now it works perfectly except for one small glitch. When I set the Navigation Drawer Icon (ic_drawer) to replace the regular "HomeAsUp" caret icon, I still get the arrow. The Nav Drawer icon does not show. I have implemented every method that was on the android developers website. But it doesn't seem to work.

我正在为我的应用程序实现一个导航抽屉。现在它完美运行,除了一个小故障。当我设置导航抽屉图标 (ic_drawer) 来替换常规的“HomeAsUp”插入符号图标时,我仍然得到箭头。导航抽屉图标不显示。我已经实现了 android 开发者网站上的所有方法。但它似乎不起作用。

Below is my code:

下面是我的代码:

DrawerLayout mDrawerLayout;
FrameLayout leftDrawer, rightDrawer, contentFrame;
ActionBarDrawerToggle mDrawerToggle;

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

private void initializeViews() {
    // TODO Auto-generated method stub
    mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout, R.drawable.ic_drawer,
            R.string.drawer_open_content_desc,
            R.string.drawer_close_content_desc);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    leftDrawer = (FrameLayout) findViewById(R.id.drawerLeft_frame);
    rightDrawer = (FrameLayout) findViewById(R.id.drawerRight_frame);
    contentFrame = (FrameLayout) findViewById(R.id.content_frame);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

回答by Naveen Kumar

I know it is quite late to answer this but this would help someone atleast.

我知道现在回答这个问题已经很晚了,但这至少会对某人有所帮助。

You should probably add these lines of code to show that navigation icon.

您可能应该添加这些代码行来显示导航图标。

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

回答by Filipe Brito

Simply put this code in your styles.xml file:

只需将此代码放在您的 styles.xml 文件中:

<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>

I had this same problem and this worked for me.

我有同样的问题,这对我有用。

EDIT

编辑

Programmatically you can set: getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

您可以以编程方式设置: getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

回答by Cabezas

You have to put this code:

你必须把这个代码:

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

or

或者

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

I hope that this code help you.

我希望这段代码对你有帮助。

回答by Atul O Holic

I solved the same issue by having a <meta-data>specified for my activity but the android:value points to the same Activity.

我通过<meta-data>为我的活动指定一个来解决同样的问题,但 android:value 指向同一个活动。

Hence, if say Activity name is MainActivity then add the below tag to your activity in the manifest file.

因此,如果说活动名称是 MainActivity,则将以下标签添加到清单文件中的活动中。

<meta-data
     android:name="android.support.PARENT_ACTIVITY"
     android:value="com.example.MainActivity" />

Hope this helps. :)

希望这可以帮助。:)

回答by Alex Deac

Check in the AndroidManifest.xml, for the that specific activity NOTto have the meta-data "android.support.PARENT_ACTIVITY"set. Try removing the <meta-data>if it's set like this:

检查在AndroidManifest.xml,对于特定活动具有的元数据"android.support.PARENT_ACTIVITY"集。<meta-data>如果它是这样设置的,请尝试删除:

<activity> 
...
 <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.company.project.ParentActivity"/>
</activity>