我已经在Kotlin应用中为每个片段创建了自定义操作栏标题。但是有时在显示自定义标题之前,它会在一瞬间显示fragmentname。
这是每个片段中的代码:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = inflate<FragmentHomeBinding>(
inflater,
R.layout.fragment_home, container, false
)
////
//some binding
////
return binding.root
}
override fun onAttach(context: Context) {
super.onAttach(context)
(activity as AppCompatActivity).supportActionBar?.title = "Custom"
}
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).supportActionBar?.title = "Custom"
}
原来我可以只编辑Navigation.xml中的标签来省去很多麻烦