2014-03-29 2 views
0

지도를 표시하는 응용 프로그램 (지도는 캔바스 배경 임)을 만들고 캔버스에 원을 추가하여 사용자를 지역화합니다 (그리기 원 사용). 캔버스 위에 단추를 추가하고 싶습니다 (사용자가지도를 클릭하면지도에 버튼을 그리고 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> 

그리고 여기 내 활동 자바 코드

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

이 생성자를 추가하려면이

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

같이해야한다)) –

답변

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> 

그리고 자바 클래스

@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의 내부 클래스이기 때문에, 경로 유용한 대답은 동의 및/또는 그것을 찬성 투표하십시오 찾을 경우에도 :-) 당신의 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 클래스를 확장하는 중 오류가 발생했습니다 문제가 manifest의

+0

나는 mapView를 선언 할 때 매개 변수로 전달할 컨텍스트와 관련이있을 것이라고 생각합니다. –

+0

@MelodieGauthier 필요 없음 '선언문 '에 선언하십시오. 그것은 활동이 아닙니다. 내 편집 된 답변을 확인하십시오. 작동하지 않는 경우 알려주십시오 :) –

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

나는 여기서하고있는 일을 얻는다. 배경에있는 상대적인 레이아웃보다 투명하게 상대 할 수있다. 하지만 여기서 문제는 캔버스, 내 캔버스는 내 단추 위에 있으며 나는 그것을 볼 수 없습니다. –