java - Android Canvas 上的弹出窗口

标签 java android eclipse

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

这是我的 Activity 的 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>

这是我的 Activity 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;
        }
    }


}

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

最佳答案

调用setContentView两次是行不通的。相反,您应该将 canvas View button 放在一个布局本身中,但顺序正确。相对布局中的最后一个小部件具有更高的优先级,因此如果您希望按钮位于 Canvas 顶部,您的布局应该是这样的。

<?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); 
}

显然警报对话框将位于 Canvas 顶部。希望对您有帮助!

编辑:我认为膨胀错误是由于类路径不正确造成的。由于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); 

    }

关于java - Android Canvas 上的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735089/

相关文章:

java - 如何将对象中的数据插入到 Eclipse 生成的 JTable 中? (请不要数据库)

java - 如何在计划作业之间共享数据

java - Android、Java 以正弦波移动手榴弹

java - 为什么我的 ListView 也显示按钮和编辑文本?

java - your.package.namespace - 应该改成什么?

android - 如何解决 "Didn' t find class 'com.google.android.gms.auth.GoogleAuthUtil'“错误?

android - 声明样式和样式之间的区别

java - 工具栏描述和 Eclipse RCP 中的代码之间的联系在哪里?

php - Eclipse 从类文件自动完成?

java - Java 堆分析工具的推荐?