Using context in a fragment

后端 未结 30 2682
Happy的楠姐
Happy的楠姐 2020-11-22 00:05

How can I get the context in a fragment?

I need to use my database whose constructor takes in the context, but getApplicationContext() and Fragmen

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 00:27

    Inside fragment for kotlin sample would help someone

    textViewStatus.setTextColor(ContextCompat.getColor(context!!, R.color.red))
    

    if you use databinding;

    bindingView.textViewStatus.setTextColor(ContextCompat.getColor(context!!, R.color.red))
    

    Where bindingView is initialized in onCreateView like this

    private lateinit var bindingView: FragmentBookingHistoryDetailBinding
    
    bindingView = DataBindingUtil.inflate(inflater, R.layout.your_layout_xml, container, false)
    

提交回复
热议问题