工具栏未显示在带有 google 地图的片段中

toolbar not showing up in fragment with google maps

我有一个 android 片段,我将 google 地图放入其中。我正在尝试将 android 工具栏添加到顶部。

我的 xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/primary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/statsSpin"
            android:spinnerMode="dropdown"/>


    </android.support.v7.widget.Toolbar>

    <!-- Lots of fancy layout -->

    <RelativeLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>


</RelativeLayout>

当我 运行 我的应用程序时,android 工具栏没有显示...

我的片段看起来像这样:

public class BreweryTappedMap extends Fragment {

    public BreweryTappedMap(){}

    String beerId = "";
    GoogleMap mMap;

    private SupportMapFragment fragment;
    private GoogleMap map;
    String userID;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_brewmap, container, false);
        ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle("Your Maps");

        Spinner spinner = (Spinner) rootView.findViewById(R.id.statsSpin);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.mappage, R.layout.dropdown_item);
        spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {


                switch (position) {
                    case 0:
                        break;
                    case 1:
                        // do whatever stuff you wanna do here
                        Fragment Fragment_one;
                        FragmentManager man= getActivity().getSupportFragmentManager();
                        FragmentTransaction tran = man.beginTransaction();
                        Fragment_one = new BreweryMap();
                        tran.replace(R.id.main, Fragment_one);//tran.
                        tran.addToBackStack(null);
                        tran.commit();
                        break;



                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });
        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        userID = prefs.getString("userID", null);

        //add url

        //call async to get breweries to add to



        return rootView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map, fragment).commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (map == null) {
            map = fragment.getMap();
            String url = "myURL";

            new GetVisitedBreweries(getActivity(), map).execute(url);        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/primary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/statsSpin"
        android:spinnerMode="dropdown"/>


</android.support.v7.widget.Toolbar>

<!-- Lots of fancy layout -->

<RelativeLayout
    android:id="@+id/map"
    android:layout_below="@+id/toolbar"  <----- You need this
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</RelativeLayout>


</RelativeLayout>

您正在使用 ToolBar 小部件,因此您应该在使用前设置工具栏:

Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
if (toolbar != null) {
   getActivity().setSupportActionBar(toolbar); 
   getActivity().getSupportActionBar().setTitle("Your Maps");
   // ...
}