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

前端之家收集整理的这篇文章主要介绍了android – MyLocation按钮隐藏在透明动作栏后面,如何重新定位?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在我的应用程序中添加了一个地图片段(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,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 Sherlock,但它也适用于常规的操作栏,并进行了一些修改.

原文链接:https://www.f2er.com/android/311927.html

猜你在找的Android相关文章