对话框中的MapView

问题描述 投票:2回答:3

我有一个DialogFragment显示为使用dialogFragment.show(this);的对话框

该片段的内容视图是ScrollView,底部有MapView。由于存在onInterceptTouchEventScrollViewis照顾MapView。当用作普通片段时,它工作正常。但作为对话框,滚动时会发生这种情况。

MapView走出Dialog

编辑:

这不起作用:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:layout_below="@id/view_text" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/view_map"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        map:uiRotateGestures="true"
        map:uiScrollGestures="true"
        map:uiTiltGestures="false"
        map:uiZoomControls="false"
        map:uiZoomGestures="true" />

    <View
        android:layout_width="match_parent"
        android:layout_height="160dp" />
</RelativeLayout>
android android-mapview
3个回答
2
投票

那么这是我必须遵循的步骤,当我必须在对话框或片段(或视图寻呼机)中放置地图。

先决条件:已经有游戏服务库,增加了INTERNET,WRITE_EXTERNAL_STORAGE等权限以及地图密钥和播放服务的元数据。

步骤1:为使用地图创建透明地图类此类将在透明框架布局中添加地图,以删除默认黑色图层(在某些设备中显示)。

public class TransparentMapFragment extends MapFragment {

    public TransparentMapFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view,
            Bundle savedInstance) {
        View layout = super.onCreateView(inflater, view, savedInstance);

        FrameLayout frameLayout = new FrameLayout(getActivity());
        frameLayout.setBackgroundColor(getResources().getColor(
                android.R.color.transparent));
        ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return layout;
    }

    public static TransparentMapFragment newInstance(String abc) {
        TransparentMapFragment tsf = new TransparentMapFragment();
        return tsf;
    }
}

第2步:对话框片段更改对话框片段的样式

    <style name="Theme.Default.Dialog" parent="@android:style/Theme.Dialog"></style>

    <style name="Theme.CustomDialog" parent="Theme.Default.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>

第3步:对话框布局创建一个全屏对话框,并根据需要添加填充。我在这里使用60dip

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="60dip" >

    <ScrollView [Use your lockable scroll view]
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dip"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
                android:textColor="#000"
                android:textSize="12sp" />


            <fragment
                android:id="@+id/map"
                android:name="com.app.widgets.TransparentMapFragment [Path of Transparent Map]"
                android:layout_width="match_parent"
                android:layout_height="100dip" />
        </LinearLayout>
    </ScrollView>


</FrameLayout>

第4步:设置Dialog片段类正常实现Map

public class MapDialogFragment extends DialogFragment {

    private View view;

    private GoogleMap mMap;
    private double lat;
    private double lon;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(),
                R.style.Theme_CustomDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.dialog_map, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        // Creating Full Screen
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);

        return dialog;
    }

    @Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);

        initializeViews();

    }

    private void initializeViews() {

        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((TransparentMapFragment) getActivity().getFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            mMap.getUiSettings().setZoomControlsEnabled(false);

            if (isGoogleMapsInstalled()) {
                if (mMap != null) {
                    setUpMap();
                }
            } else {
                Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("installGoogleMaps");
                builder.setCancelable(false);
                builder.setPositiveButton("install", getGoogleMapsListener());
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    }

    private void setUpMap() {

        lat = 28.6100;
        lon = 77.2300;

        final LatLng position = new LatLng(lon, lat);
        mMap.clear();
        mMap.getUiSettings().setAllGesturesEnabled(false);
        mMap.addMarker(new MarkerOptions().position(position).snippet(""));
    }

    public boolean isGoogleMapsInstalled() {
        try {
            getActivity().getPackageManager().getApplicationInfo(
                    "com.google.android.apps.maps", 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public android.content.DialogInterface.OnClickListener getGoogleMapsListener() {
        return new android.content.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=com.google.android.apps.maps"));
                startActivity(intent);

                // Finish the fragment so they can't circumvent the check
                if (getActivity() != null) {
                    Fragment fragment = (getActivity().getFragmentManager()
                            .findFragmentByTag(MapFragment.class.getName()));
                    FragmentTransaction ft = getActivity().getFragmentManager()
                            .beginTransaction();
                    ft.remove(fragment);
                    ft.commitAllowingStateLoss();
                }
            }

        };
    }

    @Override
    public void onDestroyView() {

        super.onDestroyView();
        if (getActivity() != null) {
            try {
                Fragment fragment = (getActivity().getFragmentManager()
                        .findFragmentById(R.id.map));
                FragmentTransaction ft = getActivity().getFragmentManager()
                        .beginTransaction();
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
            } catch (Exception e) {

            }
        }

    }

}

结果:

希望它会有所帮助。 :)


1
投票

这是众所周知的问题。尝试用这种结构包裹MapVieworiginal post here)。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="160dp"
    >

    <com.google.android.gms.maps.MapView
        android:id="@+id/view_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:uiRotateGestures="true"
        map:uiScrollGestures="true"
        map:uiTiltGestures="false"
        map:uiZoomControls="false"
        map:uiZoomGestures="true"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>

0
投票

对我来说,要使其正常工作,我只需要将样式添加到de dialog:

<style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

Dialog片段中的onCreateDialog方法:

 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = Dialog(activity,
            R.style.Theme_CustomDialog)
    val inflater = activity?.layoutInflater
    val view = inflater?.inflate(R.layout.poi_details_dialog_fragment, null)
    dialog.setContentView(view)

    //Putting the size of the window with margins
    val display = activity?.windowManager?.defaultDisplay
    val size = Point()
    display?.getSize(size)
    val width = size.x
    val height = size.y
    dialog.window.setLayout(width - resources
            .getDimensionPixelSize(R.dimen.margin_start_poi_dialog),
            height - resources
                    .getDimensionPixelSize(R.dimen.margin_top_poi_dialog))

    return dialog
}
© www.soinside.com 2019 - 2024. All rights reserved.