如何以编程方式更改com.google.android.gms.maps.SupportMapFragment的宽度和高度?我想你可以将它包装在一个带有match_parent的视图组中,但是如果我没有必要,我希望不要嵌套(老实说甚至不确定它是否有效)。
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/storefront_map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="250dp"
/>
为了给出上下文,我想在地图点击时将地图从一个高度扩展到另一个高度。
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
expandMap(); // not sure how to do this programatically.
}
});
任何帮助都会很棒!
答案 0 :(得分:37)
我在Programmatically set google map fragment visibility (API2)的帮助下很好地解决了这个问题。
mMapFragment = (SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.storefront_map));
ViewGroup.LayoutParams params = mMapFragment.getView().getLayoutParams();
params.height = 900;
mMapFragment.getView().setLayoutParams(params);
答案 1 :(得分:4)
这是使用onConfigurationChanged()
方法的示例:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
findViewById(R.id.storefront_map).getLayoutParams().width = 400;
}
答案 2 :(得分:3)
我发现的最快(代码行数较少):
ViewGroup.MarginLayoutParams parms = (ViewGroup.MarginLayoutParams) rlLocation.getLayoutParams();
parms.bottomMargin = 100;
parms.topMargin = 100;
答案 3 :(得分:1)
你不应该修复地图的高度......根据屏幕高度设置它。宽度。
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
ViewGroup.LayoutParams params = mMapFragment.getView().getLayoutParams();
params.height = height/2;
mMapFragment.getView().setLayoutParams(params);