Set a title in Toolbar from fragment in Android

后端 未结 17 805
逝去的感伤
逝去的感伤 2021-01-31 03:40

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my

相关标签:
17条回答
  • 2021-01-31 04:33

    If you are using a custom toolbar, this will help you:

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    toolbar.setTitle("Feedback");
    
    0 讨论(0)
  • 2021-01-31 04:34

    The answer is writen below in the oncreateview method of fragments.

    getActivity().setTitle("your name");
    
    0 讨论(0)
  • 2021-01-31 04:34

    This work for me :

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.id_toolbar);
    
    toolbar.setTitle("New Title");
    
    0 讨论(0)
  • 2021-01-31 04:38

    For me the problem was that for some reason the label was overwritten. I had to change it back to the string resource, in

     navigation.xml 
    

    inside the fragment tag;

    android:label="@string/android_trivia"
    
    0 讨论(0)
  • 2021-01-31 04:39

    This has worked for me, in Kotlin. Put this in your fragment class:

    if (activity != null) {
            (activity as MainActivity).supportActionBar?.title = getString(R.string.action_history)
        }
    
    0 讨论(0)
提交回复
热议问题