How to get Toolbar from fragment?

后端 未结 10 1806
梦毁少年i
梦毁少年i 2020-11-28 01:45

I have ActionBarActivity with NavigationDrawer and use support_v7 Toolbar as ActionBar. In one of my fragments toolbar has custom view

相关标签:
10条回答
  • 2020-11-28 02:15

    For Kotlin users (activity as AppCompatActivity).supportActionBar?.show()

    0 讨论(0)
  • 2020-11-28 02:23

    if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below.

    MainActivity activity = (MainActivity) getActivity();
    

    then use activity for further implementation like below

    ImageView vRightBtn = activity.toolbar.findViewById(R.id.toolbar_right_btn);
    

    Before calling this, you need to initialize your custom toolbar in your MainActivity as below.

    First set define your toolbar public like

    public Toolbar toolbar;
    public ActionBar actionBar;
    

    and in onCreate() Method assign the custom toolbar id

    toolbar = findViewById(R.id.custom_toolbar);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    

    That's It. It will work in Fragment.

    0 讨论(0)
  • 2020-11-28 02:26

    From your Fragment: ( get Toolbar from fragment?)

    // get toolbar
    ((MainAcivity)this.getActivity()).getToolbar();  // getToolbar will be method in Activity that returns Toolbar!!  don't use getSupportActionBar for getting toolbar!!
    // get action bar
    this.getActivity().getSupportActionBar();
    

    this is very helpful when you are using spinner in Toolbar and call the spinner or custom views in Toolbar from a fragment!

    From your Activity:

    // get toolbar
    this.getToolbar();
    // get Action Bar
    this.getSupportActionBar();
    
    0 讨论(0)
  • 2020-11-28 02:31

    You need to cast your activity from getActivity() to AppCompatActivity first. Here's an example:

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
    

    The reason you have to cast it is because getActivity() returns a FragmentActivity and you need an AppCompatActivity

    In Kotlin:

    (activity as AppCompatActivity).supportActionBar?.title = "My Title"
    
    0 讨论(0)
  • 2020-11-28 02:34

    Maybe you have to try getActivity().getSupportActionBar().setTitle() if you are using support_v7.

    0 讨论(0)
  • 2020-11-28 02:34

    In XML

     <androidx.appcompat.widget.Toolbar
      android:id="@+id/main_toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:layout_scrollFlags="scroll|enterAlways">
     </androidx.appcompat.widget.Toolbar>
    

    Kotlin: In fragment.kt -> onCreateView()

    setHasOptionsMenu(true)
    
    val toolbar = view.findViewById<Toolbar>(R.id.main_toolbar)
    
    (activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
    
    (activity as? AppCompatActivity)?.supportActionBar?.show()
    

    -> onCreateOptionsMenu()

       override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
           inflater.inflate(R.menu.app_main_menu,menu)
           super.onCreateOptionsMenu(menu, inflater)
       }
    

    ->onOptionsItemSelected()

       override fun onOptionsItemSelected(item: MenuItem): Boolean {
            return when (item.itemId) {
                 R.id.selected_id->{//to_do}
                 else -> super.onOptionsItemSelected(item)
            }
        }
    
    0 讨论(0)
提交回复
热议问题