工具栏没有显示在谷歌地图的片段中

时间:2015-03-10 15:09:45

标签: android android-fragments android-maps-v2 android-toolbar

我有一个安卓片段,我将谷歌地图带入。我想在顶部添加一个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);        }
    }
}

2 个答案:

答案 0 :(得分:4)

<?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>

答案 1 :(得分:1)

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

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