弹出窗口在画布Android上

时间:2014-03-29 18:29:04

标签: java android eclipse

我正在构建一个显示地图的应用程序(地图是画布背景),并通过在画布上添加圆圈(使用绘制圆圈)来本地化用户。我想在画布上添加一个按钮(现在在地图上绘制一个按钮,如果用户点击它就检查ontouch()),当触摸按钮时,我想要一个窗口弹出窗口。它工作但弹出窗口在画布后面(我可以看到它的一小部分(我删除它))。有没有办法让我的画布在按钮和弹出窗口后面?我看到人们谈论将画布放在相对布局中,但我不知道该怎么做。

这是我的活动的xml,非常简单:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/umap2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button" />

  </RelativeLayout>

这是我的活动java代码(我删除了一些与我的问题无关的事情)

  package com.example.client;

  import java.util.LinkedList;
  //....
  import java.util.Timer;

  public class Campus extends Activity{


final Handler myHandler = new Handler();
MapView mapv;
final Activity self = this;
Float ratioX;
Float ratioY;
int width;
int height;
static boolean out=false;
Intent i;


//creating a linked list for each hall
    static LinkedList<compMac> DMS = new LinkedList<compMac>();  
    static LinkedList<compMac> MCD = new LinkedList<compMac>();
    //...
    static LinkedList<compMac> SCI = new LinkedList<compMac>();   
    static LinkedList<compMac> STE = new LinkedList<compMac>();

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.campus); 
    setSize();
    this.mapv = new MapView(this);//!! my view 
    setContentView(mapv);
    i= new Intent(this, myService.class);
    this.startService(i);   
}



//*******************************View class!*******************************
public class MapView extends View {

/*
* Extract the connected users and location from the array. separate the
* array into an array for each building
* */
    private Paint redPaint;
    private float radius;
    Canvas canvas;

    public  MapView(Context context) {
        super(context) ;
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.RED);


        redPaint.setTextSize(10); 


    }
    @Override
       //drawing a point on every hall on the map where users are connected
    public void onDraw (Canvas canvas) {
                    // draw your circle on the canvas
        if(!out)
        {
    AlertDialog.Builder outOfCampus = new AlertDialog.Builder(self);
    outOfCampus.setTitle("Sorry");
        outOfCampus.setMessage("You are out of Campus");//(toDisplay);
        outOfCampus.setCancelable(false);
    outOfCampus.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    startActivity(new Intent("com.example.client.Sin"));
                }});
    AlertDialog alertdialog = outOfCampus.create();
    outOfCampus.show();
        }
        this.canvas=canvas;
        setBackgroundResource(R.drawable.umap2);
          }

    public void drawPoints(LinkedList<compMac> building)
    {
    if(!building.isEmpty())

    {
        while(!building.isEmpty())
        {
            compMac current = building.pop();   
            Float x= ratioX*(Float.parseFloat(current.getCoorX()));
            Float y= ratioY*(Float.parseFloat(current.getCoorY()));

        //  Log.w("ratioX ",(((Double)(width/768)).toString()));
        //  Log.w("ratioY ",(float)(y.toString()));
            canvas.drawCircle (x,y, 10, redPaint);

        }
    }

    }

    public  boolean onTouchEvent (MotionEvent event) {

        //...//   
            return true;
        }
    }


}

有人知道我该怎么做?感谢

2 个答案:

答案 0 :(得分:1)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/umap2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/btn_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="@drawable/back_transparent_pressed" >

    <Button
        android:id="@+id/button1"
        android:layout_centerInParent="true"
         />
</RelativeLayout>

答案 1 :(得分:1)

两次拨打setContentView无效。相反,您应该将canvas viewbutton放在一个布局中,但需要正确排序。相对布局中的最后一个窗口小部件具有更高的优先级,因此如果您希望按钮位于画布顶部,则布局应该是这样的。

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/umap2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <com.example.client.MapView
    android:id="@+id/mapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button" />

  </RelativeLayout>

并在java类中访问MapView

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campus); 
        setSize();
        this.mapv = findViewById(R.id.mapView); //!! my view 
        i= new Intent(this, myService.class);
        this.startService(i); 
}

显然alert dialog将位于画布之上。希望它有所帮助!

编辑:我认为膨胀错误是由于错误的类路径造成的。由于MapViewCampus的内部类,因此路径应该是这样的

<com.example.client.Campus.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

同时将此构造函数添加到MapView

public  MapView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet) ;
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.RED);


        redPaint.setTextSize(10); 

    }