我在我的应用程序中添加了一个地图片段(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神探夏洛克,但它也适用于常规动作栏,只需做几处修改。
发布于 2013-06-18 01:25:15
下面(特别是在fixMapControlLocations中)我已经用ActionBarSherlock解决了这个问题。
我遇到的问题是在狭小的屏幕上,以及拆分的动作条有错误的偏移量取决于旋转。isNarrow检查神探夏洛克让我知道它是否狭窄。
另一个关键的改变是我正在设置myLocation的父视图的填充。这将获取内部的所有控件,而基于层次结构的查看器是google地图所做的。Google属性徽标位于Surface对象树上的下一个父节点上。看起来不像这样很容易移动,所以我可能最终会放松底部操作栏的透明度效果,以保持遵从性。
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setUpMapIfNeeded();
getSupportActionBar().setBackgroundDrawable(d);
getSupportActionBar().setSplitBackgroundDrawable(d);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (map == null) {
// Try to obtain the map from the SupportMapFragment.
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getExtendedMap();
// Check if we were successful in obtaining the map.
if (map != null) {
setUpMap();
}
}
}
private void setUpMap() {
fixMapControlLocations();
.....
}
private void fixMapControlLocations() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
View myLocationParent = ((View)mapFragment.getView().findViewById(1).getParent());
View myLocationParentParent = ((View)myLocationParent.getParent());
myLocationParentParent.setPadding(0, actionBarHeight, 0, isNarrow()?actionBarHeight:0);
}
public boolean isNarrow() {
return ResourcesCompat.getResources_getBoolean(getApplicationContext(),
R.bool.abs__split_action_bar_is_narrow);
}发布于 2013-12-05 21:14:48
您可以通过最近添加的GoogleMap.setPadding()方法来完成这一任务:
map.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);在API文档中:
此方法允许您在地图上定义一个可见区域,通过在地图的四个边缘上设置填充来向地图发出信号,使地图边缘周围的部分可能被模糊。地图功能将适应填充。例如,缩放控件、指南针、版权声明和Google徽标将被移动以适应所定义的区域,摄像机的移动将相对于可见区域的中心,等等。
还可以看到对GoogleMap中填充工作方式的描述。
https://stackoverflow.com/questions/14299435
复制相似问题