Chr*_*ier 1 android google-maps
我有一个创建为dialogFragment的对话框,它具有自己的XML布局。是否可以将带有引用的地图嵌入DialogFragment内,以便我可以更新它的位置?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andrew.neighborlabour.CreateJobDialog">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<TextView
android:id="@+id/tvTitle"
android:text="Create Job"
android:textAppearance="@color/colorPrimaryDark"
android:textSize="24dp"
android:padding="20dp"
android:lines="2"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<ScrollView android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="400dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Address"
android:id="@+id/etAddress"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Job"
android:id="@+id/btCreate"
android:onClick="Create"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
到XML中,我可以看到地图,但是我不确定如何从Java代码中与它进行交互。
dialogFragment:
public class CreateJobDialog extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_create_job, container, false);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
创建对话框:
CreateJobDialog createJobDialog = new CreateJobDialog();
createJobDialog.show(this.getFragmentManager(), "NoticeDialogFragment");
Run Code Online (Sandbox Code Playgroud)
小智 6
我会在@Daniel Nugent 的答案中添加方法中以下代码的实现onDestroyView()。
@Override
public void onDestroyView() {
super.onDestroyView();
assert getFragmentManager() != null;
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
Run Code Online (Sandbox Code Playgroud)
我按照他的回答步骤操作,但标记为“致命例外”。当打开>关闭>返回打开'DialogFragment'。
Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #44: Duplicate id 0x7f0900c3, tag null, or parent id 0x7f090067 with another fragment for com.google.android.gms.maps.SupportMapFragment.
Run Code Online (Sandbox Code Playgroud)
注意:对于与我有相同问题的其他人来说,遵循这个答案可能会很有用。我不把我的观察放在评论中,因为我不遵守 50 声望点。
这很标准,只需使用getMapAsync()方法即可获取对Google Map的引用。
唯一令人惊讶的是,我不得不用getFragmentManager(),而不是getChildFragmentManager()在DialogFragment代码内。通常,在将SupportMapFragment嵌套在Fragment内时,您需要使用子FragmentManager,但对于DialogFragment显然是不同的。
这是DialogFragment代码:
import android.support.v4.app.DialogFragment;
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.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapDialogFragment extends DialogFragment
implements OnMapReadyCallback{
GoogleMap mMap;
public MapDialogFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_map_dialog, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return rootView;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng latLng = new LatLng(37.7688472,-122.4130859);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mMap.addMarker(markerOptions);
}
}
Run Code Online (Sandbox Code Playgroud)
启动DialogFragment:
new MapDialogFragment().show(getSupportFragmentManager(), null);
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
2298 次 |
| 最近记录: |