mac*_*nix 11 android google-maps google-maps-android-api-2
我已经在我的应用程序中添加了一个地图片段(API v2),其中地图覆盖整个屏幕,顶部是一个半透明的操作栏.该活动使用android:windowActionBarOverlay设置为true 的主题.
我还在地图上启用了"MyLocationButton",但由于地图覆盖了屏幕的整个高度,因此按钮被操作栏覆盖.
如何使地图片段在操作栏下方或屏幕底部绘制位置按钮?
不要创建自己的按钮,只需根据操作栏大小移动内置按钮.
这段代码适合我,按钮就是按钮的位置(就像谷歌地图一样):
// 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,但它也适用于常规操作栏,只需进行一些修改.
归档时间: |
|
查看次数: |
4533 次 |
最近记录: |