Android Api 23 Change Navigation View headerLayout textview

后端 未结 7 1923
无人及你
无人及你 2020-12-01 04:11

I am testing the Navigation Drawer sample project in android and i have a problem setting the text in navigation view profile header.

This is my code:

MainAc

相关标签:
7条回答
  • 2020-12-01 04:45
    View headerView=navigationView.getChildAt(0);
    TextView tvName=(TextView)hView.findViewById(R.id.name);
    TextView tvEmail=(TextView)hView.findViewById(R.id.email);
    
        tvName.setText(data);
        tvEmail.setText(data);
    
    0 讨论(0)
  • 2020-12-01 04:50

    I had the same issue and was able to avoid it with this code:

        View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
        navigationView.addHeaderView(header);
        TextView text = (TextView) header.findViewById(R.id.textView);
        text.setText("HELLO");
    
    0 讨论(0)
  • 2020-12-01 05:00

    Orium answer works and this will work as well.

    View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
        TextView text = (TextView) header.findViewById(R.id.textView);
        texto.setText("HELLO");
    
    0 讨论(0)
  • 2020-12-01 05:06

    Or if you use this attribute:

    <android.support.desibn.widget.NavigationView>
        ...
        app:headerLayout="@layout/your_header_layout"/>
    

    You can:

    View header = mNavigationView.getHeaderView(0);
    TextView title = (TextView) header.findViewById(R.id.your_title_id);
    //Or something else
    
    0 讨论(0)
  • 2020-12-01 05:08

    Now with the 23.1.1 release of the design support library, you can use

    View header = navigationView.getHeaderView(0)
    TextView text = (TextView) header.findViewById(R.id.textView);
    
    0 讨论(0)
  • The above answer is right but in kotlin, you need to use findViewById<View>

       val navigationHeader = navigation_view.getHeaderView(0)
            val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
            navigationTitle.text = "This is your header"
    
    0 讨论(0)
提交回复
热议问题