我正在开发使用谷歌地图的应用程序。我使用地图显示路线,我想在绘制路线时在地图视图上显示视图。在路线绘制时,视图不应该是可见的。问题是当绘制路线并将视图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 :(得分: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>