更改地图视图中指南针的位置

Lib*_*ony 3 android google-maps

在此处输入图片说明

我需要在应用程序中从左到右更改指南针的位置。它总是显示在左侧。是否可以更改位置?

  // change compass position
    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);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        rlp.rightMargin = rlp.leftMargin;
                        rlp.topMargin = 25;
                        view.requestLayout();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

Lal*_*dar 5

这是对齐右上角的指南针按钮所需的解决方案:

View compassButton = mMapView.findViewWithTag("GoogleMapCompass");//to access the compass button
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_END);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_START,0);
rlp.topMargin = 50;
Run Code Online (Sandbox Code Playgroud)

这将改变它的RelativeLayout参数以与右上角对齐。

结果:

在此处输入图片说明