【问题标题】:How to replace Toolbar Title with CustomView如何用 CustomView 替换工具栏标题
【发布时间】:2020-05-21 21:14:16
【问题描述】:

如何用自定义布局视图替换工具栏中的标题?然后我想在我关闭它时删除它。所以我只希望该视图在一个片段的工具栏中。

我正在尝试这个,但它不起作用:

activity.supportActionBar!!.setDisplayShowCustomEnabled(true);
activity.supportActionBar!!.setDisplayShowTitleEnabled(false);
activity.supportActionBar!!.customView = layoutInflater.inflate(R.layout.view_segmented_control, null, false)

【问题讨论】:

  • 您的问题解决了吗?
  • @mcflysoft 是的,看看我现在发布的答案

标签: java android kotlin toolbar layout-inflater


【解决方案1】:

这就是解决方案:

val activity = activity ?: return
val mainActivity = activity as MainActivity

mainActivity.supportActionBar?.setDisplayShowCustomEnabled(true)
mainActivity.supportActionBar?.setDisplayShowTitleEnabled(false)

mainActivity.supportActionBar?.customView = layoutInflater.inflate(R.layout.view_segmented_control, null, false)
segmentedControl = layoutInflater.inflate(R.layout.view_segmented_control, null, false)
segmentedControl.segmented_control.notifyConfigIsChanged()

val toolbar = mainActivity.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
toolbar.addView(segmentedControl, toolbar.childCount)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多