Abd*_*hab 5 android google-maps google-maps-android-api-2 supportmapfragment
我观察到谷歌地图的意外行为.问题是使用支持地图片段从顶部和底部区域获取裁剪,如果我使用全屏,则地图完全可见,如图1所示,但如果我应用地图片段高度或权重属性,则地图不完全可见如图2所示.
图片1的布局xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.paki.venturedive.awtest.MapsActivity" />
Run Code Online (Sandbox Code Playgroud)
布局xml为图片2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
tools:context="com.paki.venturedive.awtest.MapsActivity" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello world"
android:layout_weight="0.4" />
Run Code Online (Sandbox Code Playgroud)
Java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
Run Code Online (Sandbox Code Playgroud)
在图2中,我(用户将)无法看到格陵兰岛上方的北极海洋,同样底部区域将会丢失,如图3所示
.
有没有人面对这个问题或有没有人知道如何应对它?
任何参考链接或提示将不胜感激.
使用这个片段。我明白你的意思,通过将地图片段放入线性/相对布局中,地图会从顶部和底部被剪切,并且某些部分不可见。由于您希望地图占据屏幕的 60%,一种可能的解决方案是,将高度作为 match_parent 的 FrameLayout 内的地图片段。
现在,将 LinearLayout 中的文本视图(父级为 FrameLayout)设置为底部,并使用DisplayMetrics类将其高度动态设置为屏幕的 40%(或您的要求是多少)。通过这种方式,我们确保为地图提供全屏,以便不会剪切任何部分,并在地图上方显示文本。我还附上了更新后的屏幕截图。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_framecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/fragment_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="@+id/tv_main">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_map"
android:layout_gravity="center"
android:background="#ffffff"
android:text="Hello world" />
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
onCreate 中用于 DisplayMetrics 的 java 代码:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mylinearlayout.setMinimumHeight((int)(metrics.heightPixels * 0.4));
Run Code Online (Sandbox Code Playgroud)
我认为这会有所帮助。