NavigationDrawer图标没有显示出来 [英] NavigationDrawer icon not showing up
问题描述
我在执行 NavigationDrawer
的一个很简单的例子。该示例工作正常,但我有一个奇怪的问题。对于NavigationDrawer图标(一般 ic_drawer
)不会在动作条
显示出来。我已经使用不同的图标图像试过,但并没有达到预期的结果。这里的code。该项目只有一个名为活动 MainActivity.java
。
公共类MainActivity延伸活动{私人DrawerLayout mDrawerLayout;
私人的ListView mListView;
私人ActionBarDrawerToggle mDrawerToggle;
私有String []数组=新的String [] {你好,HOLA,合十礼
赛俩目};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer);
mListView =(ListView控件)findViewById(R.id.list); getActionBar()setDisplayHomeAsUpEnabled(真)。
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line,数组);
mListView.setAdapter(适配器);
mListView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
Toast.makeText(getApplicationContext(),数组[ARG2]
Toast.LENGTH_SHORT).show(); }
}); mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
R.drawable.ic_drawer,R.string.open,R.string.close){
@覆盖
公共无效onDrawerClosed(查看drawerView){
// TODO自动生成方法存根 invalidateOptionsMenu();
} @覆盖
公共无效onDrawerOpened(查看drawerView){
// TODO自动生成方法存根 invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
// TODO自动生成方法存根 如果(mDrawerToggle.onOptionsItemSelected(项目))
返回true;
返回super.onOptionsItemSelected(项目);
}}
如果我注释掉与code getActionBar()行setDisplayHomeAsUpEnabled(真);
中,code停止工作,这意味着 NavigationDrawer
不开。
有关参考,此活动的XML布局是:
activity_main.xml中
块引用>< android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /抽屉
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <的TextView
机器人:ID =@ + ID /帧
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:文本=更改aaaega,哼哼laaenge/> < ListView控件
机器人:ID =@ + ID /列表
机器人:layout_width =240dp
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:choiceMode =singleChoice/>< /android.support.v4.widget.DrawerLayout>有什么建议?
感谢。
解决方案在程序中包含code的这一部分,
@覆盖
保护无效onPostCreate(捆绑savedInstanceState){
super.onPostCreate(savedInstanceState);
//同步onRestoreInstanceState后切换状态已经发生。
mDrawerToggle.syncState();
} @覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){ //调用mDrawerToggle.onOptionsItemSelected(),如果返回true
//那么它处理的应用程序图标触摸事件 如果(mDrawerToggle.onOptionsItemSelected(项目)){
返回true;
}
返回super.onOptionsItemSelected(项目);
}I am implementing a very simple example of
NavigationDrawer
. The example works fine, but I have a strange problem. The icon for NavigationDrawer (generallyic_drawer
) does not show up in theActionBar
. I have tried using different icon images, but didn't achieved the desired result. Here's the code. The project has only one activity namedMainActivity.java
.public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListView mListView; private ActionBarDrawerToggle mDrawerToggle; private String[] array = new String[] { "Hello", "hola", "namaste", "salaam" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); mListView = (ListView) findViewById(R.id.list); getActionBar().setDisplayHomeAsUpEnabled(true); ArrayAdapter<String> adapter = new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_dropdown_item_1line, array); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), array[arg2], Toast.LENGTH_SHORT).show(); } }); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.open, R.string.close) { @Override public void onDrawerClosed(View drawerView) { // TODO Auto-generated method stub invalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { // TODO Auto-generated method stub invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if (mDrawerToggle.onOptionsItemSelected(item)) return true; return super.onOptionsItemSelected(item); } }
And if I comment out the line with code
getActionBar().setDisplayHomeAsUpEnabled(true);
, the code stops working, meaningNavigationDrawer
does not open.For reference, the xml layout for this activity is:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent" android:text="change aaaega, hum laaenge" /> <ListView android:id="@+id/list" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" /> </android.support.v4.widget.DrawerLayout>
Any suggestions ?
Thanks.
解决方案Include this part of code in your program,
@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); } @Override public boolean onOptionsItemSelected(MenuItem item) { // call mDrawerToggle.onOptionsItemSelected(), if it returns true // then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); }
这篇关于NavigationDrawer图标没有显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!