简体   繁体   English

如何在片段更新中更新工具栏标题

[英]How do I update a toolbar title on fragment update

I am setting up an activity that hosts fragments so what i want is to update a toolbar title in my activity whenever a fragment transaction happens. 我正在设置一个托管片段的活动,因此,每当发生片段事务时,我想要的是在活动中更新工具栏标题。 Currently only the first fragment transactions upates the title but the following transaction are not able to do so despite that part of my code being reached can any one help? 目前,只有第一个片段事务会更新标题,但后续事务却无法这样做,尽管我的代码部分已经到达了,可以帮上忙吗?

so here is some code that I have tried. 所以这是我尝试过的一些代码。

from the activity, I set up a method called setUp toolbar like so 从活动中,我建立了一个名为setUp工具栏的方法,如下所示

private void setUp(String title) {
    toolbar=binding.authtoolbar;
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(title);
}

which I called from the fragment like so. 我从片段中这样称呼它。

((MyActivtyClass)getActivity()).setUp(toolbar)

then I later changed to 然后我改成了

    toolbar=binding.authtoolbar;
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(title); 

and in the fragment, I change it to this 在片段中,我将其更改为

  ((MyActivtyClass)getActivity()).getSupportActionBar().setTitle("myTitle");

but still nothing. 但还是一无所有。 This issue has taken up a lot of my time and I​ would appreciate if I could get some help on this 这个问题占用了我很多时间,如果能在这方面得到帮助,我将不胜感激

尝试这个

getActivity().setTitle("your title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 在处理带片段的导航时更新活动中的工具栏标题 - Android - update toolbar title in activity while handle back navigation with fragment 如何在片段中更新ListView? - How do I update a ListView in a Fragment? 如何更新片段中的 recyclerView - How do I update a recyclerView in a fragment 如何从服务动态更新工具栏中菜单项的标题 - How can I update the title of a menu item in the toolbar dynamically from a service 如何更新片段中的 recyclerview 列表? (notifyDataSetChanged 不起作用) - How do I update the recyclerview list in the fragment? (notifyDataSetChanged not working) 如何隐藏片段中工具栏中的OptionMenu? - How do I hide the OptionMenu in a toolbar from a fragment? 离开片段后更新ActionBar标题 - Update ActionBar title after leaving fragment Android设置片段工具栏标题 - Android setting fragment toolbar title 在我的活动(SQLite)中修改数据后如何更新我的片段? - How do I update my Fragment after data modification in my activity (SQLite)? 在活动/片段中确认警报对话框后,如何在单击时更新我的​​ RecyclerView 按钮状态? - How do I update my RecyclerView button state on click after alert dialog confirmation in activity/fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM