我做了一個NavigationView
,並在我的代碼中包含getSupportActionBar().setDisplayHomeAsUpEnabled(true);
來顯示漢堡包按鈕,它工作。但隨後我刪除了默認工具欄(通過將AppTheme
設置爲styles
值爲parent="Theme.AppCompat.Light.NoActionBar"
),並在佈局中實現了我自己的工具欄。現在,而不是漢堡按鈕,它顯示後退按鈕,但點擊它可以繪製NavigationView
。我該怎麼辦?在自定義工具欄上顯示漢堡包圖標?
我的Java代碼:
private DrawerLayout sideBar;
private ActionBarDrawerToggle sideBarToggle;
private Toolbar actionToolbar;
@Override
public void onCreate(...) {
sideBar = (DrawerLayout) findViewById(R.id.sqliteLayout);
sideBarToggle = new ActionBarDrawerToggle(this, sideBar, actionToolbar, R.string.sideBarOpen, R.string.sideBarClose);
actionToolbar = (Toolbar) findViewById(R.id.navAction);
sideBar.addDrawerListener(sideBarToggle);
sideBarToggle.syncState();
setSupportActionBar(actionToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(sideBarToggle.onOptionsItemSelected(item)){
return true;}
return super.onOptionsItemSelected(item);
}
我的工具欄代碼:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/navAction"
android:background="@color/colorPrimary"
app:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar">
</android.support.v7.widget.Toolbar>
SOLUTION:
我重新排序的Java代碼這一點,並在new ActionBarDrawerToggle(...)
取出第三個參數和有效!
actionToolbar = (Toolbar) findViewById(R.id.navAction);
setSupportActionBar(actionToolbar);
sideBar = (DrawerLayout) findViewById(R.id.sqliteLayout);
sideBarToggle = new ActionBarDrawerToggle(this, sideBar, R.string.sideBarOpen, R.string.sideBarClose);
sideBar.addDrawerListener(sideBarToggle);
sideBarToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);