Android 如何以编程方式更改 Google Maps v2(支持 Fragment)的宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21469345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:11  来源:igfitidea点击:

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

androidandroid-fragmentsandroid-maps-v2

提问by loeschg

How do you programatically change the width and height of a com.google.android.gms.maps.SupportMapFragment? I'd imagine you might be able to wrap it in a viewgroup with match_parent, but I'm hoping not to nest if I don't have to (honestly not even sure it works).

如何以编程方式更改 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"
                    />

To give context, I'd like to expand the map from one height to another on map click.

为了提供上下文,我想在地图点击时将地图从一个高度扩展到另一个高度。

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
       expandMap(); // not sure how to do this programatically.
    }
});

Any help would be great!

任何帮助都会很棒!

回答by loeschg

I figured it out largely with the help of Programmatically set google map fragment visibility (API2).

我主要是在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);

回答by Jorgesys

this is an example using onConfigurationChanged()method :

这是使用onConfigurationChanged()方法的示例:

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

回答by Diogo Rosa

Fastest (with less code lines) way i found:

我发现的最快(代码行更少)方式:

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

回答by Ashish John

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);