1、创建一个公共的Toolbar
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 设置高度为 ActionBar 的高度 -->
<!-- 背景颜色为 ActionBar 的背景颜色 -->
<!-- 返回按钮的图标 -->
<android.support.v7.widget.Toolbar
android:id="@+id/public_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<TextView
android:id="@+id/public_tool_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLength="7"
android:textColor="#ffffff"
android:textSize="@dimen/title_size"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
</FrameLayout>
2、新建Activity基类
/**
* Created by ww on 2016/3/8. time : 10:26
*/
public abstract class ToolBarActivity extends AppCompatActivity {
protected Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (null == activity) {
activity = getActivity();
}
initContentView();
ButterKnife.bind(activity);
initView();
initAction();
}
protected abstract Activity getActivity();
/**
* 获取页面ResID
*
* @return
*/
protected abstract int onContentView();
/**
* 初始化UI
*/
protected abstract void initView();
protected abstract void initAction();
/**
* 创建一个根视图
*
* @return
*/
private void initContentView() {
/* 直接创建一个帧布局,作为视图容器的父容器 */
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
setContentView(layout);
if (onToolbarResID() != 0) {
View view = inflater.inflate(onToolbarResID(), layout, false);
layout.addView(view);
}
if (onContentView() != 0) {
View view = inflater.inflate(onContentView(), layout, false);
layout.addView(view);
}
}
/**
* 获取toolBarResID
*
* @return
*/
protected abstract int onToolbarResID();
}
3、继承BaseToobarActivity
public class MapActivity extends CustomBaseActivity implements OnMarkerClickListener{
@Bind(R.id.bmapView)
MapView mMapView;
@Override
protected Activity getActivity() {
return this;
}
@Override
protected int onContentView() {
return R.layout.activity_map;
}
@Override
protected void initView() {
BaiduMap mBaiduMap = mMapView.getMap();
// //普通地图
// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
// 卫星地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
//设置地图的缩放级别.
MapStatusUpdateFactory.zoomTo(15.0f);
// //空白地图,
// 基础地图瓦片将不会被渲染。在地图类型中设置为NONE,将不会使用流量下载基础地图瓦片图层。使用场景:与瓦片图层一起使用,节省流量,提升自定义瓦片图下载速度。
// mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);
LatLng pt1 = new LatLng(39.93923, 116.357428);
LatLng pt2 = new LatLng(39.91923, 116.327428);
LatLng pt3 = new LatLng(39.99929, 116.347428);
LatLng pt4 = new LatLng(39.99923, 116.367428);
LatLng pt5 = new LatLng(39.91929, 116.387428);
List<LatLng> pts = new ArrayList<LatLng>();
pts.add(pt1);
pts.add(pt2);
pts.add(pt3);
pts.add(pt4);
pts.add(pt5);
// 构建用户绘制多边形的Option对象
OverlayOptions polygonOption = new PolygonOptions().points(pts).stroke(new Stroke(5, 0xAA00FF00)).fillColor(0xAAFFFF00);
// 在地图上添加多边形Option,用于显示
mBaiduMap.addOverlay(polygonOption);
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
final LatLng latLng = arg0.getPosition();
Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_SHORT).show();
return true;
}
});
}
Marker marker = null;
OnMarkerClickListener listener = new OnMarkerClickListener() {
/**
* 地图 Marker 覆盖物点击事件监听函数
* @param marker 被点击的 marker
*/
@Override
public boolean onMarkerClick(Marker arg0) {
Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_SHORT).show();
return false;
}
};
@Override
protected void initAction() {
}
@Override
protected int onToolbarResID() {
return R.layout.public_toolbar;
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
public boolean onMarkerClick(Marker arg0) {
Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_SHORT).show();
return false;
}
}