How do you programmatically change the width & height of Google Maps v2 (support Fragment)?

烂漫一生 提交于 2019-11-29 05:35:46
loeschg

I figured it out largely with the help of 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);

this is an example using onConfigurationChanged() method :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    findViewById(R.id.storefront_map).getLayoutParams().width = 400;
}

Fastest (with less code lines) way i found:

ViewGroup.MarginLayoutParams parms = (ViewGroup.MarginLayoutParams) rlLocation.getLayoutParams();
    parms.bottomMargin = 100;
    parms.topMargin = 100;

You should not fix height of a map... set it according to screen height & width.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!