问题
I am trying to have SupportMapFragment with a custom layout, right now the following code works :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = super.onCreateView(inflater, container, savedInstanceState);
// initMap();
return view;
}
But I want to change the onCreateView()
method to inflate a different layout, instead of calling the super.onCreateView()
method.
To look like something like this:
View view = inflater.inflate(R.layout.fragment, container,false);
mMap = (GoogleMap)view.findViewById(R.id.mapview);
But GoogleMap is not a view
so I cannot add it to the xml, like the following way:
<com.google.android.gms.maps.GoogleMap
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
回答1:
If you want to create a SupportMapFragment with a custom layout, I prefer to extend the android.support.v4.app.Fragment
from the Support Library and inflate your custom XML with the com.google.android.gms.maps.MapView
and other views there.
But be careful to forward the livecycle methods to this MapView
as quoted here.
回答2:
I added a LinearLayout above the map. I used this layout for the buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/llPlaces"
android:orientation="horizontal"
android:background="@color/ACGris">
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON1"/>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON2"/>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON3"/>
And then I created a class which extends SupportMapFragment
public class FrgMapas extends SupportMapFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View viewMain = super.onCreateView(inflater, container, savedInstanceState);
View viewButtons = inflater.inflate(R.layout.layout_buttons, null);
FrameLayout mainChild = (FrameLayout) ((ViewGroup)viewMain).getChildAt(0);
mainChild.addView(viewButtons);
Button btn1 = (Button) viewMain.findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "TuTa :)", Toast.LENGTH_LONG).show();
}
});
return viewMain;
}
Hope this works for you
回答3:
com.google.android.gms.maps.MapView
is a View
that you can embed in your layouts.
回答4:
You can override onCreateView in the class you derive from SupportMapFragment. You can call super.onCreateView and then insert the result into a custom ViewGroup you inflater or instantiate. Here's some sample code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.my_layout,
container, false);
View v = super.onCreateView(inflater, container, savedInstanceState);
layout.addView(v, 0);
return layout;
}
来源:https://stackoverflow.com/questions/13804511/supportmapfragment-with-a-custom-layout-possible