简体   繁体   中英

Title, Menu items not showing on ActionBar inside a fragment

I added the custom toolbar in xml and then added it as Actionbar in fragment. But it's not showing anything from Title to Menus. The only actionbar is visible

Here is my xml code

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <!-- Must be last for right layering display -->
    <FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar2"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary" />

    </FrameLayout>

</FrameLayout>

And here is my java code

public class searchView extends Fragment{

    Toolbar search;
    MaterialSearchView search_view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.search_view,container,false);
        FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.search_view, container, false);

        search = (Toolbar)layout.findViewById(R.id.toolbar2);
        ((AppCompatActivity) getActivity()).setSupportActionBar(search);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
        setHasOptionsMenu(true);

        return v;
    }
}

In these code I called the fragment in MainActivity On Bottombar click

  else if (position==1){
                    searchView f = new searchView();
                    getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,f).commit();
                }
                return true;
Toolbar search;
MaterialSearchView search_view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.search_view,container,false);
    setHasOptionsMenu(true);
    search = (Toolbar)v.findViewById(R.id.toolbar2);
    ((AppCompatActivity) getActivity()).setSupportActionBar(search);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");

    return v;
}

}

to show options menu override

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    //add your  menu here;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM