我想将文本设置为导航抽屉标题文本,我搜索了很多但是每个人都通过膨胀布局来实现它,但对我来说它创建了2个标题,所以有没有办法设置它而不会给布局充气?
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header=navigationView. findViewById(R.id.header);
/*View view=navigationView.inflateHeaderView(R.layout.nav_header_main);*/
name = (TextView)header.findViewById(R.id.username);
email = (TextView)header.findViewById(R.id.email);
name.setText(personName);
email.setText(personEmail);`
答案 0 :(得分:165)
您可以通过navigationView.getHeaderView(0)获取导航标题视图,请尝试以下代码
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
/*View view=navigationView.inflateHeaderView(R.layout.nav_header_main);*/
name = (TextView)header.findViewById(R.id.username);
email = (TextView)header.findViewById(R.id.email);
name.setText(personName);
email.setText(personEmail);