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

2017-05-25  本文已影响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
    }
}
上一篇 下一篇

猜你喜欢

热点阅读