gpt4 book ai didi

android - 如何将 FragmentActivity 转换为 Fragment?

转载 作者:行者123 更新时间:2023-11-29 01:40:55 26 4
gpt4 key购买 nike

我是 Android 新手。我正在构建一个带有滑动条格式的应用程序,滑动条的一部分是一个 fragment 。我可以将 Activity 转换为 Fragment,但现在我有一个 Fragment Activity,我想将它转换为 Fragment。我可以在 Android 中执行吗?感谢您的帮助。

   import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class GoogleMapV2 extends FragmentActivity implements LocationListener {
GoogleMap googleMap;

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

// Getting Google Play availability status
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());

// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available

int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();

} else { // Google Play Services are available

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapGoogle);

// Getting GoogleMap object from the fragment
googleMap = fm.getMap();

// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);

// Getting LocationManager object from System Service
// LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();

// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);

if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
}

@Override
public void onLocationChanged(Location location) {

// TextView tvLocation = (TextView) fa.findViewById(R.id.tv_location);

// Getting latitude of the current location
double latitude = location.getLatitude();

// Getting longitude of the current location
double longitude = location.getLongitude();

// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);

// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

// Setting latitude and longitude in the TextView tv_location
// tvLocation.setText("Latitude:" + latitude + ", Longitude:" +
// longitude);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

}


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyLocation" />

<fragment
android:id="@+id/mapGoogle"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</FrameLayout>

最佳答案

抱歉,您不能这样做,因为 fragment 是应用程序的用户界面或行为的一部分,可以放置在 Activity 中。它属于一个 Activity 。所以Activity不能转化为Fragment。

更多信息查看以下链接:

关于android - 如何将 FragmentActivity 转换为 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com