我正在尝试实施类似谷歌地图的网络版本的信息窗口。 我创建了一个扩展线性布局的自定义布局,并使用TextView和按钮填充它。
我已经覆盖了balloonLayout的dispatchDraw方法(它扩展了LinearLayout)以绘制一个带圆角和气球尖端的容器。
在我的自定义叠加层上,我将点击坐标保存到变量,然后在先前保存的坐标处的覆盖绘制方法上绘制信息窗。信息窗口绘制正常,但按钮失去焦点,从不调用它的onClick监听器,它可以被点击(它没有得到按黄色状态),我无法理解为什么......
提前谢谢。
这是我的balloonlayout.class dispatchdraw方法:
@Override
protected void dispatchDraw(Canvas canvas)
{
Paint panelPaint = new Paint();
panelPaint.setARGB(0, 0, 0, 0);
RectF panelRect = new RectF();
panelRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(panelRect, 5, 5, panelPaint);
RectF baloonRect = new RectF();
baloonRect.set(0,0, getMeasuredWidth(), 2*(getMeasuredHeight()/3));
panelPaint.setARGB(230, 255, 255, 255);
canvas.drawRoundRect(baloonRect, 10, 10, panelPaint);
Path baloonTip = new Path();
baloonTip.moveTo(5*(getMeasuredWidth()/8), 2*(getMeasuredHeight()/3));
baloonTip.lineTo(getMeasuredWidth()/2, getMeasuredHeight());
baloonTip.lineTo(3*(getMeasuredWidth()/4), 2*(getMeasuredHeight()/3));
canvas.drawPath(baloonTip, panelPaint);
super.dispatchDraw(canvas);
}
这是ballonlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.preciotaxi.balloonLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/transparent_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5px"
android:paddingTop="5px"
android:paddingRight="5px"
android:paddingBottom="5px"
android:orientation="vertical"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ballonLabel"
android:text="coordenadas"
/>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ballonButton"
android:text="Ir aquí"
android:clickable="true"
/>
</com.forgottenprojects.preciotaxi.balloonLayout>
tapOverlay.class
class tapOverlay extends Overlay
{
public GeoPoint lastTap=null;
private Context context;
LayoutInflater inflater;
balloonLayout noteBaloon;
public tapOverlay(Context c)
{
this.context=c;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
noteBaloon = (balloonLayout) inflater.inflate(R.layout.balloonlayout, null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200,100);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
noteBaloon.setLayoutParams(layoutParams);
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
lastTap = p;
mapView.getController().animateTo(p);
return true;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
Projection p = mapView.getProjection();
Paint paint = new Paint();
paint.setColor(0xFFFFFF00);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
if(lastTap!=null)
{
drawInfoWindow(canvas, p, shadow, mapView);
}
super.draw(canvas, mapView, shadow);
}
private void drawInfoWindow(Canvas canvas, Projection myprojection,
boolean shadow, MapView mapView)
{
try{
mapView.removeView(noteBaloon);
noteBaloon.setVisibility(View.VISIBLE);
TextView textmsg = (TextView) noteBaloon.findViewById(R.id.ballonLabel);
textmsg.setText(lastTap.getLatitudeE6()+","+lastTap.getLongitudeE6());
Button btn = (Button) noteBaloon.findViewById(R.id.ballonButton);
btn.setFocusable(true);
btn.setOnClickListener(btn_onclick);
mapView.addView(noteBaloon, new MapView.LayoutParams(200,200,lastTap,MapView.LayoutParams.BOTTOM_CENTER));
mapView.setEnabled(true);
mapView.invalidate();
} catch (Exception e)
{
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private OnClickListener btn_onclick = new OnClickListener() {
//This never shows...
@Override
public void onClick(View v) {
Toast.makeText(context, "Se hizo clic... y ahora?", Toast.LENGTH_LONG).show();
}
};
}