sinat_26761191 2017-07-20 09:34 采纳率: 27.8%
浏览 7222
已采纳

kotlin在fragment中不能像activity中直接通过id调用控件了,有什么办法吗

kotlin中 在fragment中不能像activity中直接通过id调用控件了,有什么办法吗

  • 写回答

3条回答 默认 最新

  • metgo 2017-07-20 10:21
    关注

    在fragment中复写这个 onCreateView的时候实例化view,用view去findViewById,语法可以参考下边的

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val view = inflater!!.inflate(R.layout.xxxx, container, false)
            val iv_card = view.findViewById(R.id.xxxx) as ImageView
            iv_card.setImageResource(xxxxxxxxx])
            return view
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?