我正在为我的应用实现导航抽屉.现在除了一个小故障外它完美无缺.当我设置导航抽屉图标(ic_drawer)来替换常规的"HomeAsUp"插入符号图标时,我仍然得到箭头.导航抽屉图标不显示.我已经实现了android开发者网站上的每个方法.但它似乎没有用.
以下是我的代码:
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); }
Naveen Kumar.. 45
我知道现在回答这个问题已经很晚了,但这对任何人都有帮助.
您应该添加这些代码行来显示导航图标.
@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); }
在我的情况下,这就是我所缺少的.谢谢.AndroidManifest中不需要元数据.接受的答案是不正确的. (2认同)
Filipe Brito.. 20
只需将此代码放在styles.xml文件中:
- @drawable/ic_drawer
- @drawable/ic_drawer
我有同样的问题,这对我有用.
编辑
以编程方式,您可以设置:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
我知道现在回答这个问题已经很晚了,但这对任何人都有帮助.
您应该添加这些代码行来显示导航图标.
@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); }
只需将此代码放在styles.xml文件中:
<item name="homeAsUpIndicator">@drawable/ic_drawer</item> <item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
我有同样的问题,这对我有用.
编辑
以编程方式,您可以设置:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
你必须把这段代码:
getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
要么
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
我希望这段代码可以帮到你.