我正在创建一个 Skobbler 注释,如下所示:
markerCoords = mapView.pointToCoordinate(skScreenPoint); //gives us the coordinate
SKAnnotation annotation = new SKAnnotation(11);
SKAnnotationView view = new SKAnnotationView();
View v = getLayoutInflater().inflate(R.layout.annotation_marker, null, false);
v.findViewById(R.id.btn_destination).setOnClickListener(destListener);
v.findViewById(R.id.btn_origin).setOnClickListener(originListener);
view.setView(v);
annotation.setAnnotationView(view);
annotation.setLocation(mapView.pointToCoordinate(skScreenPoint));
annotation.setMininumZoomLevel(1);
mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_POP_OUT);
View R.layout.annotation_marker
包含几个按钮,但我无法点击/单击它们。我的点击穿过注释并点击 map (我已经检测到它)。当我膨胀 View 时,我尝试在 View 上使用 requestFocus() ,但这没有效果。我还在 xml 中添加了 android:clickable='true'
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_gravity="center_horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Origin"
android:id="@+id/btn_origin"
android:layout_gravity="right"
android:focusable="true"
android:clickable="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:text="Destination"
android:id="@+id/btn_destination" />
</LinearLayout>
如何让点击事件点击按钮而不是底层 map ?
最佳答案
由于 onClick 事件不会发生,为了在 map 上的不同 View 上启用事件,解决方案是使用 commentViews 将它们合并到不同的注释中,并在 onAnnotationSelected 回调上执行操作(因此每个注释应该有一个 View ) )。
为了使事件以自然的方式触发,必须为注释设置相应的偏移量,以便在单击实际 View 时触发 onAnnotationSelected 回调,而不是注释的扩展。
@Override
public void onSurfaceCreated(SKMapViewHolder mapHolder) {
….
SKAnnotation annotation = new SKAnnotation(137);
SKAnnotationView annotationView = new SKAnnotationView();
View v = getLayoutInflater().inflate(R.layout.bug_2_btn_layout,null,false);
annotationView.setView(v);
annotation.setAnnotationView(annotationView);
annotation.setLocation(new SKCoordinate(52.520008,13.404954));
annotation.setMininumZoomLevel(1);
double viewToLayoutRatio = getResources().getDimension(R.dimen.originBtn_width)/getResources().getDimension(R.dimen.annotationLayout_width);
annotation.setOffset(new SKScreenPoint((float) (annotation.getOffset().getX()-(viewToLayoutRatio*getResources().getDimension(R.dimen.originBtn_width))), (float) (annotation.getOffset().getY()+(viewToLayoutRatio/3)*getResources().getDimension(R.dimen.originBtn_width))));
mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_POP_OUT);
…
}
@Override
public void onAnnotationSelected(final SKAnnotation annotation) {
switch (annotation.getUniqueID()) {
…
case 137:
Toast.makeText(getApplicationContext(),"Annotation was clicked",Toast.LENGTH_SHORT);
View v = getLayoutInflater().inflate(R.layout.bug_2_btn_layout,null,false);
originClicked(v);
break;
}
}
关于android - 带按钮的 Skobbler 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35198915/