前提:
val headerView: View = LayoutInflater.from(this).inflate(R.layout.common_header, null)
错误示例:
import kotlinx.android.synthetic.main.common_header.*错误引入
headerView.hintTextView.text = getHint()错误写法1
hintTextView.text = getHint()错误写法2
正确写法:
1,正确写法一,findviewById
val hintTextView: TextView = headerView.findViewById(R.id.hintTextView)
2,正确写法二,引入
import kotlinx.android.synthetic.main.common_header.View*,正确引入
headerView.hintTextView.text = getHint()
原因:headerView不是Activity里边的View,是你自己生成的一个View,所以需要引入这个layout布局文件中所有的View,用headerView去点,或者你用headerView去findViewById
博客介绍了Android开发中headerView使用的相关内容。给出了错误示例,如错误引入和错误写法,同时给出两种正确写法,一是用findViewById,二是正确引入布局文件中所有View。还说明了错误原因,headerView是自定义View,需特殊处理。
2274

被折叠的 条评论
为什么被折叠?



