在地图上创建自定义指南针或移动本机按钮

Create custom compass on map or move native button

我尝试将指南针按钮的位置从左上角更改为左下角,但不能。

据我所知,有很多创建自定义按钮和禁用本机按钮的建议,但没有一个完整且正常的示例。

如何在android地图上添加自定义罗盘按钮?

您可以更改内置指南针的位置。试试这个代码:

ViewGroup parent = (ViewGroup) mapView.findViewById(Integer.parseInt("1")).getParent();
        View compassButton = parent.getChildAt(4);
        /* now set position compass */
        RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
        rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_START);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
        Resources r = context.getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics());
        rlp.setMargins(0, (int)px,0, 0); // 160 la truc y , 30 la  truc x
        px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());
        rlp.setMarginEnd((int)px);

        compassButton.setLayoutParams(rlp);

只需使用 RelativeLayout 参数并将其设置在所需位置