美文网首页
Kotlin使用过程中的零散知识点

Kotlin使用过程中的零散知识点

作者: 蛋黄酥酥 | 来源:发表于2017-05-25 14:33 被阅读75次
  1. 使用'kotlin-android-extensions'可以直接引用xml的资源id使用,不用再find。但要注意,通过inflate得到的(没有setContentView)会遇到view为空的报错,比如在Fragment里,就不能直接引用id,得用find
class NoteFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater!!.inflate(R.layout.fragment_note, container, false)
        //tv_hello.text = "hello world" //这句,会报tv_hello view为空的崩溃
        view.find<TextView>(R.id.tv_hello).text = "hello world"
        return view
    }
}

相关文章

网友评论

      本文标题:Kotlin使用过程中的零散知识点

      本文链接:https://www.haomeiwen.com/subject/rdwaxxtx.html