在片段活动中添加工具栏

问题描述:

当前我正在处理谷歌地图api。我想添加工具栏/操作栏到我的地图片段。在片段活动中添加工具栏

请检查下面的图片链接: -

This is the Screen shot please check this out

这是我的布局文件的.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width`enter code here`="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.android.bhavin04.MapsActivity" 
     /> 

我MaintActivity延伸FragmentActivity并实现OnMapReadyCallback。 如何将工具栏添加到我的主要活动。一个帮助将不胜感激。

主要活动代码:

package com.android.bhavin04.ganeshutsavmumbai; 

import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Point; 
import android.location.LocationManager; 
import android.provider.Settings; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.widget.Toolbar; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.UiSettings; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.pushbots.push.Pushbots; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    private LatLngBounds Mumbai = new LatLngBounds(
      new LatLng(-44, 113), new LatLng(19.075984, 72.877656)); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     Pushbots.sharedInstance().init(this); 




     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     mMap = googleMap; 

     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Mumbai.getCenter(), 14)); 

     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 
     // mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     mMap.setMyLocationEnabled(true); 
     mMap.setTrafficEnabled(true); 

     LatLng Mumbai = new LatLng(19.075979, 72.879696); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(Mumbai)); 


    } 
    } 

ActionBarActivity,而不是FragmentActivity拓展业务。 (MainActivity延伸ActionBarActivity

ActionBarActivityFragmentActivity延伸,因此它将工作。

使用AppCompatActivity这是从FragmentActivity延伸,你可以设计你的布局类似如下:

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

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="?attr/colorPrimary" 
    android:elevation="6dp" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

<FrameLayout 
    android:id="@+id/container_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/toolbar"> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.android.bhavin04.MapsActivity" /> 
</FrameLayout> 
</RelativeLayout> 

上面的XML代码包含Toolbar可无论如何定制你想和可作为ActionBar

+0

它的作品非常感谢你:) :) –

+0

@BhavinHimmatGoswami请接受答案 –