【发布时间】:2021-12-30 04:45:00
【问题描述】:
我正在制作一个电影应用程序,我想将操作栏颜色更改为透明但仅限于特定片段。
我想在 theme.xml 中制作一个操作栏样式,但如果不更改整个应用操作栏,我就无法做到。
我考虑过制作一个自定义工具栏,然后为它分配一个后退按钮,但操作栏与我制作的自定义工具栏一起显示。
我正在使用 MVVM。
我尝试执行以下操作,但它会将操作栏颜色更改为整个应用程序,而不仅仅是这个片段:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view =
inflater.inflate(com.dapps.moviesmvvm.R.layout.movie_details_fragment, container, false)
toolbar = view.findViewById<android.widget.Toolbar>(R.id.toolbar)
setHasOptionsMenu(true)
// Change the action bar to red for the **whole** application,not just the fragment.
val actionbar = (activity as AppCompatActivity).supportActionBar
actionbar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(context!!, android.R.color.holo_red_dark)));
toolbar.setTitle(R.string.app_name)
return view
}
仅在此特定片段中更改操作栏颜色的最佳做法是什么?
谢谢!
编辑:
尝试将操作栏设置为 Color.Transparent 时,颜色改为深灰色。
【问题讨论】:
-
此片段分离时您也需要还原它。如果您进一步使用替换事务,可能在
onDestoyView。 -
您需要在您所在片段的
onDestroyView或下一个/上一个片段的onResume中将您的操作栏颜色更改回正常。第一个选项最有可能被使用。 -
谢谢你们!
标签: android kotlin android-fragments android-actionbar android-toolbar