查看不显示地图查看
问题描述:
我正在开发使用谷歌地图的应用程序。我使用地图显示路线,并且我想在路线绘制时在地图视图上显示视图。在路线绘制时,视图不应该可见。问题是当绘制路线时,我将视图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();
}
任何帮助将不胜感激。
答
如果你想覆盖项目,然后使用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
你有什么错误吗?它在调试中起作用吗?如果在XML中将可见性设置为true,您是否可以获得视图以显示所需的方式? – 2014-10-26 20:37:19
不,我没有得到任何错误。我只在现在调试时才尝试它,最后是如果我在xml中将可见性设置为true,则视图在地图上可见。此外,如果我直接在onCreate方法中将可见性设置为true(无需等待超时),它也可以正常工作。 – 2014-10-26 20:53:41