我有两个xml标头文件,我想在用户登录之前显示其中一个,在登录后显示另一个标头。
我正在通过共享首选项检查登录状态,但是我不知道如何更改标头xml文件。
最佳答案
一种方法是删除当前的标题视图(removeHeaderView)并膨胀新的标题视图并将其设置为NavigationView
(inflateHeaderView),如下所示:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
// If the view has not been set in the xml then here should be a null check because there is going to be no header view
// and there will be no need to remove that
View hView = navigationView.getHeaderView(0);
navigationView.removeHeaderView(hView);
navigationView.inflateHeaderView(R.layout.nav_my_other_header);
HIH