2014-10-26 99 views
1

我正在開發使用谷歌地圖的應用程序。我使用地圖顯示路線,並且我想在路線繪製時在地圖視圖上顯示視圖。在路線繪製時,視圖不應該可見。問題是當繪製路線時,我將視圖VISIBLE標誌設置爲true,視圖不會在地圖上顯示。查看不顯示地圖查看

這是我使用的佈局。正如你可以看到彈出的初始狀態是INVISIBLE

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <com.google.android.gms.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    <ImageView 
     android:id="@+id/popup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/popup" 
     android:visibility="invisible" /> 
</RelativeLayout> 

爲簡單起見,我不會張貼整個Java代碼,但只有onCreate方法的一部分。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // stuff that is not so important 
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { 
    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      /* 
      The next line is the source of all evil. I use the sleep to simulate the 
      time needed to draw the route. The problem is that if I wait for a while the 
      map get initialized and when set the view to VISIBLE nothing happens - the 
      view is not shown over the map. If I use 1 ms for sleep timeout the map is 
      still not initialized and the view is shown correctly over the map. 
      */ 
      Thread.sleep(1000); 

     } catch (InterruptedException e) {} 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { 
     View view = findViewById(R.id.popup); 
     view.setVisibility(View.VISIBLE); 
    } 
    task.execute(); 
} 

任何幫助將不勝感激。

+0

你有什麼錯誤嗎?它在調試中起作用嗎?如果在XML中將可見性設置爲true,您是否可以獲得視圖以顯示所需的方式? – 2014-10-26 20:37:19

+0

不,我沒有得到任何錯誤。我只在現在調試時才嘗試它,最後是如果我在xml中將可見性設置爲true,則視圖在地圖上可見。此外,如果我直接在onCreate方法中將可見性設置爲true(無需等待超時),它也可以正常工作。 – 2014-10-26 20:53:41

回答

0

如果你想覆蓋項目,然後使用FrameLayout而不是RelativeLayout。地圖使用的是match_parent,它佔用了整個屏幕並且不允許顯示視圖。試試這個吧,

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <com.google.android.gms.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    <ImageView 
     android:id="@+id/popup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/popup" 
     android:visibility="invisible" /> 
</FrameLayout> 
+0

問題不在父佈局中。如果視圖可見,則一切正常,因此父級佈局正常。 – 2014-10-27 21:47:18