【问题标题】:popup window over canvas Android画布Android上的弹出窗口
【发布时间】:2014-05-09 05:29:22
【问题描述】:

我正在构建一个应用程序,它显示地图(地图是画布背景)并通过在画布上添加圆圈(使用绘制圆圈)来本地化用户。我想在画布上添加一个按钮(现在在地图上绘制一个按钮,如果用户单击它,请使用 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;
        }
    }


}

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

【问题讨论】:

  • 如果您觉得答案有用,请接受或/并投票赞成:-)))

标签: java android eclipse


【解决方案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); 

    }

【讨论】:

  • 谢谢!它还没有工作,但我认为它可能是解决方案的一部分。我有以下错误:Error inflating class com.example.client.MapView 你认为问题出在清单的
  • 我认为这可能与我想在声明 mapView 时作为参数传递的上下文有关
  • @MelodieGauthier 无需在manifest 中声明。这不是一项活动。检查我编辑的答案。如果它不起作用,请告诉我:)
  • ooups 我写了清单,但意思是 xml 布局。它不起作用...我也尝试使用 com.example.client.Campus$MapView,因为 MapView 是 Campus 的内部类。我认为 MapView 必须是静态的 (stackoverflow.com/questions/5828801/…)。但是我的班级正在调用几个非静态方法。我想我会在我的地图上使用一个“绘制”按钮,并在用户点击它时显示一个警报对话框。我这周必须介绍我的项目,所以我没有很多时间:S 再次感谢您的帮助,我认为使用静态就可以了
  • 最好将 MapView 单独的类/文件 (MapView.java) 设置为内部类。你不必把它变成静态的。
【解决方案2】:
<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>

【讨论】:

  • 我明白你在这里做什么,一个透明的相对布局在背景中的其他相对布局之上。但是这里的问题是画布,我的画布在我的按钮上方,我看不到它。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2018-08-10
  • 1970-01-01
相关资源
最近更新 更多