Kotlin从NavigationView标头获取视图 [英] kotlin get view from NavigationView header

查看:173
本文介绍了Kotlin从NavigationView标头获取视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的变体,但我相信有更好的方法来实现

I have such variant, but I believe that there is a better way to do this

val header = nav_view.getHeaderView(0) as LinearLayout
val userName = header.findViewById(R.id.user_name) as TextView

问题是,当我尝试仅通过user_name获取它时,它就是null.

The issue is that when I try to get it just by user_name, it is null.

推荐答案

您可以考虑使用Android-Kotlin扩展插件.在这种情况下,您的代码将更加简单,并且将是:

You might consider using Android-Kotlin extension plugin. In that case your code will be much simpler and would be:

val headerView: View = nav_view.getHeaderView(0)
headerView.userNameTxt.text = "Some Value"

这篇关于Kotlin从NavigationView标头获取视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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