如何设置文本以从导航抽屉中的抽屉页眉布局查看而不膨胀视图

时间:2022-12-17 19:36:52

I want to set text to the navigation drawers headers text, I searched a lot but everyone does it by inflating a layout but for me it creates 2 headers so is there any way to set it without inflating the layout?

我想将文本设置为导航抽屉标题文本,我搜索了很多但是每个人都通过膨胀布局来实现它,但对我来说它创建了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);`

1 个解决方案

#1


155  

You get navigation header view by navigationView.getHeaderView(0) , try code below

您可以通过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);

#1


155  

You get navigation header view by navigationView.getHeaderView(0) , try code below

您可以通过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);