只有在导航抽屉打开时,主页按钮才会从抽屉图标更改为向上旋转按钮

时间:2017-08-14 14:46:05

标签: java android android-fragments android-actionbar navigation-drawer

我在我的应用程序中实现了一个导航抽屉,但大多数情况下它似乎正常工作。

我将主页按钮设置为导航抽屉图标(汉堡包),这在正常旋转中仍然存在。从图1(肖像)和图像2(旋转后的风景)可以看出。 Image 1

Image 2

但是,如果我打开导航抽屉,然后旋转屏幕然后关闭导航抽屉,主页按钮将变为向上按钮,如图3所示。 Image 3

我尝试过更改周围的东西,以便它始终保持汉堡包图标,但没有运气。

我的 MainActivity 代码如下。

public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private String mDrawerTitle;
private String mTitle;
ActionBarDrawerToggle mDrawerToggle;
private static int sNavPosition = 0;
private static final String NAV_POSITION_KEY = "navKey";
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerTitle = getTitle().toString();
    mTitle = getTitle().toString();
    mNavigationDrawerItemTitles = getResources().getStringArray(R.array.drawer_fragment_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    setUpToolbar();
    NavDrawerItem[] navDrawerItems = new NavDrawerItem[4];

    navDrawerItems[0] = new NavDrawerItem(getString(R.string.nav_home));
    navDrawerItems[1] = new NavDrawerItem(getString(R.string.nav_aims));
    navDrawerItems[2] = new NavDrawerItem(getString(R.string.nav_arttrail));
    navDrawerItems[3] = new NavDrawerItem(getString(R.string.nav_submission));

    DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.drawer_list_item,navDrawerItems);
    View header = getLayoutInflater().inflate(R.layout.drawer_header,null);
    mDrawerList.addHeaderView(header);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    setUpDrawerToggle();
    if (savedInstanceState != null && savedInstanceState.containsKey(NAV_POSITION_KEY)){
        sNavPosition = savedInstanceState.getInt(NAV_POSITION_KEY);
    }
    new DrawerItemClickListener().selectItem(sNavPosition);
}

private void setUpDrawerToggle(){
    mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name,R.string.app_name);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerToggle.syncState();
}



@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

private void setUpToolbar(){
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}



@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putInt(NAV_POSITION_KEY,sNavPosition);
}

}

我已经排除了有关点击侦听导航的不必要代码。

非常感谢任何帮助或建议。谢谢。

1 个答案:

答案 0 :(得分:2)

尝试删除getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   来自你的mDrawerToggle.setDrawerIndicatorEnabled(true); setUpDrawerToggle();方法

Edit: setDisplayHomeAsUpEnabled(false) hides the default back button