1

我用AndroidStudio創建了地圖的自動活動。現在如何在活動中插入SupportMapFragment?

<RelativeLayout 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:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.mitic.mappa.MapsActivity" 
    android:id="@+id/contenitore" 
    android:background="#ccebff"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="#3365ff" 
     android:id="@+id/navBar"> 

     <Button 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:text="Dove vuoi andare?" 
      android:layout_weight="1" 
      android:textColor="@color/common_signin_btn_dark_text_default" 
      android:id="@+id/b1" 
      android:textStyle="bold" 
      android:background="#3365ff" 
      android:onClick="scegliPercorso" 
      android:layout_marginBottom="10dp"/> 

     <Button 
      android:textColor="@color/common_signin_btn_dark_text_default" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:text="Mappa generale" 
      android:id="@+id/b2" 
      android:layout_weight="1" 
      android:textStyle="bold" 
      android:onClick="visualizzaMappaGenerale" 
      android:background="#3365ff" /> 

     <Button 
      android:textColor="@color/common_signin_btn_dark_text_default" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:text="Informazioni UnisaMapp" 
      android:layout_toRightOf="@id/b2" 
      android:layout_weight="1" 
      android:textStyle="bold" 
      android:onClick="info" 
      android:background="#3365ff" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/navBar" 
     android:id="@+id/layout2"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="#ffffff" 
      android:id="@+id/contFragment"> 

     </LinearLayout> 
    </RelativeLayout> 

    <fragment 
     android:layout_below="@id/layout2" 
     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"/> 


</RelativeLayout> 

我希望把這個活動中的一類,以便調用此方法startResolutionForResult(getActivity(),REQUEST_LOCATION)(getActivity()我不能把它FragmentActivity)。

這是我的課MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, 
      GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{ 

     private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
     final static int REQUEST_LOCATION = 199; 

     private static final LatLng FISCIANO = new LatLng(40.77502, 14.79018); 
     private String providerId = LocationManager.GPS_PROVIDER; 
     private String providerNetwork = LocationManager.NETWORK_PROVIDER; 
     private LocationManager locationManager = null; 
     private static final int MIN_DIST = 10; 
     private static final int MIN_PERIOD = 5000; 
     private GoogleMap mMap; 
     private Location mCurrentLocation; 
     private ListaItinerari itinerari; 
     private FragmentManager fm; 
     private Polyline polyline; 
     private Marker sonoQui, destinazione; 
     private ListaMarkers listaMarker; 
     private SupportMapFragment mapFragment; 
     private GoogleApiClient mGoogleApiClient; 
     private LocationRequest mLocationRequest; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      Log.d("MapsActivity", "onCreate"); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_maps); 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
      mLocationRequest = LocationRequest.create() 
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
        .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
        .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
      itinerari = new ListaItinerari(); 
      fm = getFragmentManager(); 
      mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      mapFragment.getMapAsync(this); 

     } 
public void onMapReady(GoogleMap googleMap) { 
     Log.d("MapsActivity", "ReadyMap"); 
     mMap = googleMap; 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(FISCIANO)); 
      mMap.moveCamera(CameraUpdateFactory.zoomTo(15.0f)); 
     } else { 
      Toast.makeText(getApplicationContext(), "Accetta i permessi", Toast.LENGTH_LONG).show(); 
     } 
    } 

我想在活動中添加此片段,所以我創建這個類

public class Appoggio extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_layout); 
    } 
} 

這是map_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".Appoggio" 
    android:background="#ccebff"> 

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

</RelativeLayout> 

如何在此佈局中添加MapsActivity類?
謝謝大家。

回答

0

把視圖尋呼機在主要活動,然後你的片段添加到視圖尋呼機

ViewPager pager= (ViewPager) findViewById(R.id.pager); 
TabsPagerfragment tabsPagerfragment=new TabsPagerfragment(getSupportFragmentManager()); 

     pager.setAdapter(tabsPagerAdapter); 

希望這會幫助你!

相關問題