English 中文(简体)
How to layout zoom Control with setBuiltInZoomControls(true)?
原标题:

would like to add a zoom control to the map. I also want to layout the position of the zoom Control instead of the default middle bottom position. I can do this by getZoomControl but it is deprecated.

Could anyone tell me how to do this with setBuildtInZoomControls?

问题回答

This is how I got mine working finally (by embedding a ZoomControl in XML layout).

mapView = (MapView) this.findViewById(R.id.mapView);

ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
        public void onClick(View v) {
            mapView.getController().zoomIn();
        }
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
        public void onClick(View v) {
            mapView.getController().zoomOut();
        }
});

See: Always show zoom controls on a MapView

mapView.getZoomButtonsController()

Although this is undocumented (at least in the javadoc available here: com.google.android.maps) I am quite sure this is the replacement for the deprecated getZoomControls

Edit: just found out that it is documented, just not in the google api docs but rather here: ZoomButtonsController on developer.android.com

You could then call getContainer () or getZoomControls () (depending on what you want to do) to get access to the zoom controls view.

And then do something like

RelativeLayout.LayoutParams zoomParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
zoomParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mapView.getZoomButtonsController().getZoomControls().setLayoutParams(zoomParams);

although I have to admit that I am not sure what layout the maps view uses, could be LinearLayout as well.

LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.layout_zoom);
View mapController = mapView.getZoomButtonsController().getZoomControls();
zoomLayout.addView(mapController, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

I m pretty sure that the only way to do this is to either use the MapView s deprecated getZoomControls() or to DIY it. The MapController has the methods necessary (e.g. zoomIn() and zoomOut()).

RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);

final MapView mapView = new MapView(this, DEBUG_MAP_API_KEY);
RelativeLayout.LayoutParams mapViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT );
relativeLayout.addView(mapView, mapViewLayoutParams);

RelativeLayout.LayoutParams zoomControlsLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
zoomControlsLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
zoomControlsLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

relativeLayout.addView(mapView.getZoomControls(), zoomControlsLayoutParams);

mapView.setClickable(true);
mapView.setEnabled(true);

I think this should work ... Hope this is useful :)

The solution provided by George on Placing Zoom Controls in a MapView allows to layout the built in zoom controls by using gravity attributes. This attribute positions the element into its container which seems that is positioned on the bottom of the screen so its not possible to position the zoom controls on the top of the screen by layouting the zoom controls.

In conclusion you can layout the built in zoom controls by doing:

((FrameLayout.LayoutParams) mapView.getZoomButtonsController().getZoomControls().getLayoutParams()).gravity = Gravity.RIGHT;

Remember that the zoom controls are contained into a FrameLayout therefore any try to use rules like RelativeLayout.ALIGN_PARENT_BOTTOM or RelativeLayout.ALIGN_PARENT_TOP will lead you only to pain and sorrow.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签