谷歌地图Android API:移动指南针按钮

6

我注意到可以为UI设置边距,还可以禁用按钮。

但是我们能把指南针按钮移动到不同的位置吗,而不仅仅是在左上角吗?

谷歌地图应用程序将其放在右上角,这让我想到可能是可以的。

4个回答

3

我之前也遇到了同样的问题,解决方法如下:

/* access compass */
ViewGroup parent = (ViewGroup) mMapView.findViewById(Integer.parseInt("1")).getParent();
View compassButton = parent.getChildAt(4);
/* position compass */
RelativeLayout.LayoutParams CompRlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
CompRlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
CompRlp.setMargins(0, 180, 180, 0);

这将把指南针稍微移远离顶部。


2

I fixed the problem like this :

try {
                assert mapFragment.getView() != null;
                final ViewGroup parent = (ViewGroup) mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
                parent.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            for (int i = 0, n = parent.getChildCount(); i < n; i++) {
                                View view = parent.getChildAt(i);
                                RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
                                // position on right bottom
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                rlp.rightMargin = rlp.leftMargin;
                                rlp.bottomMargin = 25;
                                view.requestLayout();
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
        }

在这个例子中,我将指南针放在右下角。在进行此操作时,请确保您的mapFragment已创建,我建议您在MapFragment的“onMapReady”方法中运行代码。

2

我也在为此苦苦挣扎。我的设计师们提到了谷歌地图作为一个例子,但我还没有找到一种方法来实现这一点。目前最接近的是JCDecary在这篇SO文章中的回答,它将按钮移到了底部左侧角落。不过它依赖于一个命名标签,这让我感到担忧,因为谷歌可能随时更改它。


2

关于2.0地图API。

    // change compass position 
if (mapView != null &&
        mapView.findViewById(Integer.parseInt("1")) != null) {
    // Get the view
    View locationCompass = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("5"));
    // and next place it, on bottom right (as Google Maps app)
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
            locationCompass.getLayoutParams();
    // position on right bottom
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    layoutParams.setMargins(0, 160,30, 0); // 160 la truc y , 30 la  truc x
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接