我在我的应用程序中添加了一个地图片段(API v2),地图覆盖了整个屏幕,顶部还有一个半透明的操作栏。该活动使用一个主题,将android:windowActionBarOverlay设置为true。
我还启用了地图上的"MyLocationButton“,但是由于地图覆盖了屏幕的全部高度,所以按钮被动作条覆盖。

如何使地图片段在操作栏下面或屏幕底部绘制位置按钮?
发布于 2013-04-01 08:28:40
不要创建自己的按钮,只需按动作条大小移动“内置”按钮即可。
这段代码适用于我,而按钮正是按钮应该在的位置(就像google地图中的那样):
// 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);
}只需将此代码放入onActivityCreated中(如果将其放入onCreateOptionsMenu中,它将不支持3.0之前的版本--因为那里的生命周期不同。
另外,"R.id.MainContainer“是映射片段的容器。
我使用的是ActionBar神探夏洛克,但它也适用于常规动作栏,只需做几处修改。
https://stackoverflow.com/questions/14299435
复制相似问题