Setting a subtitle on my Toolbar from a fragment

前端 未结 4 1017
陌清茗
陌清茗 2020-12-31 13:31

I am having some trouble setting the sub-title in my Toolbar from my fragment. I keep getting a Null Pointer Exception at the setSubTitle.

@Override
public          


        
相关标签:
4条回答
  • 2020-12-31 14:04

    You Can Create One Custom Layout For This And Then use in onCreateView method.

    private ActionBar actionBar;
    

    OnCreateView method:

    actionBar = getActivity().getActionBar();
    if (actionBar != null) {
        actionBar.setCustomView(R.layout.actionbar_inner_custom_view);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    }
    
    0 讨论(0)
  • 2020-12-31 14:15

    Declare a variable ActionBar toolbar and use it this way:

    toolbar=((AppCompatActivity) getActivity()).getSupportActionBar();
    
    toolbar.setHomeAsUpIndicator(R.drawable.ic_arrow_left);
    toolbar.setTitle("Detalhes");
    
    0 讨论(0)
  • 2020-12-31 14:21

    To use the Toolbar and the Appcompat 21, you have to use an AppCompatActivity and use:

    ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle("About");
    
    0 讨论(0)
  • 2020-12-31 14:23

    instead of calling getActivity() get the activity reference from the onAttach() lifecycle method of Fragment.

    if you are using sherlock actionbar then call getSherlock().getActionBar()

    0 讨论(0)
提交回复
热议问题