Layout to constrain size of MapView

2019-08-28 01:59发布

问题:

I'm trying to constrain the size of a MapView so that it takes up, say 90% of the screen's height and leaves the remaining 10% at the bottom for another view e.g. a menu bar. I can get the effect but the menu bar overlays the MapView so that the bottom part of the map is not visible. Although the MapView can scroll the Google logo is partly obscured (and I suspect this might be against the T & C's of the Maps API), I'd rather constrain the MapView to be contained within a designated part of the screen. So far I haven't found any layout which enforces the MapView to the normal layout constraints e.g. layout_weight.

I'm beginning to think that this can only be achieved programatically by getting the device's screen dimensions and then explicitly setting the MapView layout width and height or has anyone managed to do this with an xml layout.

回答1:

I found the answer here. It is basically what John said, use layout_weight:

<LinearLayout ... >
 <FrameLayout android:id="@+id/map_frame"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0.7" >     

    <com.google.android.maps.MapView
        android:id="@+id/map_view" 
        android:layout_width="fill_parent"
        android:layout_height="match_parent" 
        android:enabled="true" 
        android:clickable="true"             
        android:apiKey="api_key"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 </FrameLayout>
</LinearLayout>