隐藏在透明操作栏后面的MyLocation按钮,如何重新定位?

mac*_*nix 11 android google-maps google-maps-android-api-2

我已经在我的应用程序中添加了一个地图片段(API v2),其中地图覆盖整个屏幕,顶部是一个半透明的操作栏.该活动使用android:windowActionBarOverlay设置为true 的主题.

我还在地图上启用了"MyLocationButton",但由于地图覆盖了屏幕的整个高度,因此按钮被操作栏覆盖.

在此输入图像描述

如何使地图片段在操作栏下方或屏幕底部绘制位置按钮?

Or *_*zaz 6

不要创建自己的按钮,只需根据操作栏大小移动内置按钮.
这段代码适合我,按钮就是按钮的位置(就像谷歌地图一样):

// Gets the my location button
View myLocationButton = getSherlockActivity().findViewById(R.id.MainContainer).findViewById(2); 

// Checks if we found the my location button        
if (myLocationButton != null){
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();

           // Checks if the os version has actionbar in it or not
           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
              if (getSherlockActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
           }
           // Before the action bar was added to the api
           else if(getSherlockActivity().getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
           }

        // Sets the margin of the button
        ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(myLocationButton.getLayoutParams());
        marginParams.setMargins(0, actionBarHeight + 20, 20, 0);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        myLocationButton.setLayoutParams(layoutParams);
}
Run Code Online (Sandbox Code Playgroud)


只需将此代码放在onActivityCreated中(如果将它放在onCreateOptionsMenu中,它将不支持3.0之前的版本 - 因为生命周期不同.
另一方面,"R.id.MainContainer"是地图的容器片段.

我正在使用ActionBar Sherlock,但它也适用于常规操作栏,只需进行一些修改.


MH.*_*MH. 0

确保您使用?android:attr/actionBarSize(或者?attr/actionBarSize如果您正在使用 ActionBarSherlock)正确偏移片段的内容。

根据您想要实现的效果,可以将此值应用为边距或填充。我猜测,由于 ActionBar 是半透明的,您将需要尝试填充,以便仍然让地图出现在其后面(并保持透明效果)。我只是不能 100% 确定填充是否真的会将“定位我”按钮向下移动......如果不是,那么应用边距可能是您唯一的其他选择。

有关此属性的示例和更多详细信息,请参阅此处。