我使用AndroidStudio创建了地图的自动活动。
<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 :(得分:0)
在您的主要活动中放置一个视图寻呼机,然后将您的片段添加到视图寻呼机
ViewPager pager= (ViewPager) findViewById(R.id.pager);
TabsPagerfragment tabsPagerfragment=new TabsPagerfragment(getSupportFragmentManager());
pager.setAdapter(tabsPagerAdapter);
希望这会对你有帮助!