2014-03-29 82 views
0

我正在構建一個顯示地圖(地圖是畫布背景)並通過在畫布上添加圓(使用繪製圓)來本地化用戶的應用程序。我想在畫布上添加一個按鈕(現在在地圖上繪製一個按鈕,並在ontouch()中檢查用戶是否點擊了它),並且當按下按鈕時,我想要一個窗口彈出窗口。它的工作,但彈出窗口後面的畫布(我可以看到它的一小塊(我刪除它))。有沒有辦法讓我的畫布在按鈕和彈出窗口?我看到有人在討論如何將畫布放在相對佈局中,但我不知道如何做到這一點。彈出窗口覆蓋畫面Android

這裏是我的活動的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; 
     } 
    } 


} 

有人有一個想法,我可以做到這一點?謝謝

+0

如果您認爲有用的答案,請接受或/和其給予好評:-) )) –

回答

0

調用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> 

並訪問您的MapView在Java類

@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將在畫布上。希望能幫助到你!

編輯:我認爲充氣錯誤是由於不正確的類路徑。由於MapView是內部類的Campus,路徑應該是這樣的

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

    } 
+0

謝謝!它還沒有工作,但我認爲它可能是解決方案的一部分。我有以下錯誤:錯誤膨脹類com.example.client.MapView 您認爲問題出現在清單的

+0

我認爲這可能與上下文有關,當我聲明mapView –

+0

@MelodieGauthier時,我認爲不需要用'manifest'聲明它。這不是一項活動。檢查我編輯的答案。請讓我知道,如果它不工作:) –

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> 
+0

我得到你在做什麼,在背景中的其他相對佈局的透明relativelayout。但這裏的問題是畫布,我的畫布在我的按鈕上,我看不到它。 –