NavigationView中获取headerLayout的方法

1.获取NavigationView:

    private  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);  

2.获取headerLayout

View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);  

3.获取其中的组件:

TextView res = (TextView) headerLayout.findViewById(R.id.account_show);
res.setText("dasdasdasadsasdasdasdas");

注意:这样或出现重复headlayout:

删除即可解决

 app:headerLayout="@layout/nav_drawer"

原文地址:https://www.cnblogs.com/xinyibufang/p/7381653.html