NavigationView如何处理动态头内容 [英] NavigationView how to handle dynamic header content

查看:390
本文介绍了NavigationView如何处理动态头内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty标准NavigationView。当我像下面它的作品完美头使用静态布局。

I have a pretty standard NavigationView. When i use a static layout in the header like below it works perfect.

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"/>

但我想有一个动态的标头,这样thah当用户登录等我可以改变它...所以我试图用一个片段,而不是nav_header.xml

But i want to have a dynamic header so thah i can change it when user logged in etc... So i tried to use a fragment instead of nav_header.xml

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/fragment_header"
    app:menu="@menu/drawer_view"/>

我可以使用一个片段在headerLayout,所以我可以处理我的片段中的Java文件中的所有逻辑。或者有什么是处理这个问题的解决方案。

Can i use a fragment in the headerLayout so i can handle all my logic in the fragment's java file. Or what is the right solution to handle this problem.

推荐答案

您可以做到这一点通过充气自定义布局,并设置code这对导航视图的标题。

You can do that in code by inflating custom layout and set it's header for navigation view.

NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
View nav_header = LayoutInflater.from(this).inflate(R.layout.nav_header, null);
((TextView) nav_header.findViewById(R.id.name)).setText("UserName");
navigationView.addHeaderView(nav_header);

您不需要设置应用程序。headerLayout 在XML

You don't need to set app:headerLayout in xml.

这篇关于NavigationView如何处理动态头内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆