我正在实现一个非常简单的NavigationDrawer
示例。这个例子运行正常,但我有一个奇怪的问题。 NavigationDrawer的图标(通常为ic_drawer
)未显示在ActionBar
中。我尝试使用不同的图标图像,但没有达到预期的效果。这是代码。该项目只有一个名为MainActivity.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);
}
}
如果我使用代码getActionBar().setDisplayHomeAsUpEnabled(true);
注释掉该行,则代码将停止工作,这意味着NavigationDrawer
无法打开。
作为参考,此活动的xml布局为:
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>
有什么建议吗?
感谢。
答案 0 :(得分:1)
在程序中包含这部分代码,
@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);
}