【问题标题】:Kotlin Android Studio - setContenView - binding (fragment)Kotlin Android Studio - setContenView - 绑定(片段)
【发布时间】:2022-01-16 14:56:05
【问题描述】:

我尝试在 Android Studios 中制作秒表。我知道如何在活动上进行,但我需要在片段上进行。我的问题是我不知道如何在带有绑定的片段中设置内容视图。

相关代码:

class Stoppuhr : Fragment() {
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_stoppuhr, container, false)
}

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding = FragmentStoppuhrBinding.inflate(layoutInflater)

    setContentView(binding.root)//<---


    binding.startStopButton.setOnClickListener { startStopTimer() }
    binding.resetButton.setOnClickListener { resetTimer() }

    serviceIntent = Intent(getActivity(), TimerService::class.java)
    requireActivity().registerReceiver(updateTime, IntentFilter(TimerService.TIMER_UPDATED));
}}

提前致谢。

【问题讨论】:

    标签: android kotlin binding fragment setcontentview


    【解决方案1】:

    Fragments 不要使用setContentView(id)。他们有一个onCreateView 处理通货膨胀的函数,你已经在做。您应该能够检索该函数中的绑定,并像往常一样在最后返回视图。

    这个链接有一个关于与片段绑定的部分,大约在页面的一半。

    https://developer.android.com/topic/libraries/view-binding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      相关资源
      最近更新 更多