代码之家  ›  专栏  ›  技术社区  ›  Fernando Gallego

带有布局的地图视图覆盖弹出菜单失去焦点

  •  2
  • Fernando Gallego  · 技术社区  · 14 年前

    我正在尝试实现一个像googlemaps的web版本那样的infowindow。

    我已经重写了BalloLayout的dispatchDraw方法(它扩展了LinearLayout)来绘制一个带有圆角和引出序号提示的容器。

    在我的自定义overlay类中,我将tap坐标保存到一个变量中,然后在覆盖的draw方法上绘制infowindow。信息窗口画的不错,但按钮失去焦点,从来没有调用它的onClick侦听器,它可以点击(它没有得到按下的黄色状态),我不明白为什么。。。

    这是我的气球布局.classdispatchdraw方法:

    @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>
    

    以及单击Overlay.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();
        }
    };
    

    }

    0 回复  |  直到 14 年前