mei*_*teg 9 android android-layout google-maps-android-api-2
我正在尝试将Google Maps Android API v2集成到我的Android应用程序中.我正在将Google地图置于布局中间.当布局能够适合屏幕时,它的效果很好,但是当布局太大而不适合,并且用户向下滚动以查看其余内容时,其余的布局会被Google Map涂黑.请参阅以下屏幕截图:

(注意:禁用所有地图手势以允许用户滚动.)
我的布局XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20sp" >
<TextView
android:id="@+id/race_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14sp"
android:textSize="@dimen/DetailsTop"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/race_in_chase" />
<TextView
android:id="@id/race_in_chase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/in_the_chase"
android:textSize="@dimen/DetailsTop"
android:background="@drawable/inthechase"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:visibility="gone"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/race_name"
style="@style/RaceDetailsName"
android:layout_below="@id/race_num" />
<TextView
android:id="@+id/race_track"
style="@style/RaceDetailsText"
android:paddingBottom="20sp"
android:layout_below="@id/race_name" />
<TextView
android:id="@+id/race_time"
style="@style/RaceDetailsText"
android:layout_below="@id/race_track" />
<TextView
android:id="@+id/race_tv"
style="@style/RaceDetailsText"
android:layout_below="@id/race_time" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="380dp"
android:layout_marginTop="20sp"
android:layout_marginBottom="20sp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/race_tv" />
<TextView
android:id="@+id/race_track_size"
style="@style/RaceDetailsText"
android:layout_below="@id/map" />
</RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我的活动中的地图设置:
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private static final LatLng CHICAGOLAND = new LatLng(41.474856, -88.056928);
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setAllGesturesEnabled(false);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(CHICAGOLAND)
.zoom(14.5f)
.bearing(53)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Run Code Online (Sandbox Code Playgroud)
我的预感是地图片段不像ScrollView,因为如果整个布局适合屏幕并且不需要滚动,那么布局就好了.地图本身似乎是正确的大小.停电是视图中其余项目的确切大小.有没有人有任何想法,我怎么能摆脱地图下面的停电,所以我可以看到它下面的TextView?到目前为止,我所尝试的一切都没有奏效.
我能够通过 hack 解决这个问题。我扩展了 ScrollView,这样我就可以收到有关滚动事件的通知。我使用Synchronize ScrollView 滚动位置 - android中的 ObservableScrollView 实现。
我添加到我的 onCreate() 中:
ObservableScrollView sv = (ObservableScrollView) findViewById(R.id.scroll);
sv.setScrollViewListener(this);
Run Code Online (Sandbox Code Playgroud)
然后,当我收到滚动更改事件时,我会切换整个视图的可见性,这会强制重新布局,从而防止出现黑框。
public void onScrollChanged(ObservableScrollView sv, int x, int y, int oldx, int oldy) {
sv.setVisibility(View.GONE);
sv.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
我尝试使地图视图无效,并仅切换地图视图的可见性,但都不起作用。值得庆幸的是,切换整个视图的可见性不会导致任何闪烁或其他奇怪的行为。
可能有更好的解决方案,但目前,这是我发现的唯一可行的方法。