【问题标题】:Change actionBar color only in fragment仅在片段中更改 actionBar 颜色
【发布时间】: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


【解决方案1】:

在您的 MainActivity 中

 fun supportActionBar(number: Int, title: String): Boolean {
    return when (number) {
        0 -> {
            setViewVisible(
                title,
                0,
                View.GONE,
                View.GONE,
                View.GONE,
                View.GONE,
                View.GONE,
                View.VISIBLE
            )
            true
        }
        1 -> {
            setViewVisible(
                title,
                1,
                View.GONE,
                View.GONE,
                View.GONE,
                View.GONE,
                View.VISIBLE,
                View.VISIBLE
            )
            true
        }
        
        else -> true
    }
}

 private fun setViewVisible(
    title: String,
    from: Int,
    imgToolbarLeft: Int,
    imgToolbarCancel: Int,
    txtToolbarDone: Int,
    viewToolbar: Int,
    imgToolbarMainMenu: Int,
    nav_view: Int
) {
    binding.include.txtToolbarName.text = title
    binding.include.txtToolbarDone.visibility = txtToolbarDone
    binding.include.imgToolbarLeft.visibility = imgToolbarLeft
    binding.include.imgToolbarCancel.visibility = imgToolbarCancel
    binding.include.imgToolbarMainMenu.visibility = imgToolbarMainMenu
    binding.include.viewToolbar.visibility = viewToolbar
    binding.navView.visibility = nav_view

when (from) {
        1 -> {
                                bindingMain.include.toolbar.setBackgroundColor(resources.getColor(R.color.holo_red_dark))

        }else{

bindingMain.include.toolbar.setBackgroundColor(resources.getColor(R.color.holo_red)) } }

在片段中

     override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        (context as MainActivity).onSectionAttached(1, resources.getString(R.string.your_string))
    }

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多