2016-06-13 8 views
1

現在私はGoogleマップ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)); 


    } 
    } 

答えて

0

ActionBarActivity代わりのFragmentActivityからあなたの活動を拡張します。 (MainActivityはActionBarActivityを拡張)

ActionBarActivityFragmentActivityから拡張されます。

0

以下のようなものFragmentActivityから延長され、あなたのレイアウトを設計することができます使用AppCompatActivity

<?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コードは、あなたが望むとにかくカスタマイズすることができ、ActionBarとして使用することができますToolbarが含まれています。

+0

ありがとうございます:) –

+0

@BhavinHimmatGoswami答えを受け入れてください –

関連する問題