我试图让一个按钮在 SupportMapFragment 上工作,但 OnclickListener 没有触发。这是我的代码:
地图片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setRetainInstance(true);
SP = getActivity().getSharedPreferences(SHARED_PREFS_NAME, 0);
this.SP.registerOnSharedPreferenceChangeListener(this);
View view = inflater.inflate(R.layout.map, container, false);
FrameLayout frameLayout = new FrameLayout(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
((ViewGroup) view).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
setHasOptionsMenu(true);
tracker = GoogleAnalyticsTracker.getInstance();
setMapTransparent((ViewGroup) view);
String mapType = SP.getString("PREF_MAP", "MAP_TYPE_NORMAL");
FragmentManager myFragmentManager = getFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
if (mapType.equals("MAP_TYPE_NORMAL")) {
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else if (mapType.equals("MAP_TYPE_SATELLITE")) {
myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else if (mapType.equals("MAP_TYPE_HYBRID")) {
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
myMap.setMyLocationEnabled(true);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
myMap.getUiSettings().setScrollGesturesEnabled(true);
myMap.getUiSettings().setZoomGesturesEnabled(true);
((Activity) getActivity()).setSupportProgressBarIndeterminateVisibility(false);
button = (Button) view.findViewById(R.id.sbutton);
this.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "CLICK!", Toast.LENGTH_SHORT).show();
}
});
return view;
}
地图.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:holo="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main"
>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
<Button
android:id="@+id/sbutton"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center_horizontal|center_vertical"
android:background="@drawable/ic_social_share_light"
/>
不知道为什么不起作用。有人可以看看代码并告诉我我做错了什么吗?
谢谢