2014-10-27 11 views
2

我有一個DialogFragment示出爲使用dialogFragment.show(this);MapView類在對話框

該片段的內容視圖一個對話框是ScrollViewMapView在底部。由於存在MapViewonInterceptTouchEvent被照顧到ScrollView。當用作普通片段時,它工作正常。但作爲一個對話框,這是滾動時發生的事情。

enter image description here

enter image description here

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> 
+0

意味着你需要什麼?該地圖需要在對話框中? – 2014-11-03 06:53:21

+0

@AndoMasahashi當滾動時,mapview不應該出現在對話框中。當滾動出對話框時,它必須像文本的最後一行一樣隱藏。就像典型的滾動視圖一樣。 – 2014-11-03 07:00:24

+0

你可以提供示例代碼來處理它嗎? – 2014-11-03 07:01:37

回答

1

那麼這些是我用於跟蹤時,我必須把地圖對話框或片段(或視圖尋呼機)的步驟。

先決條件:已經有播放服務庫,增加了像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步:設置對話框片段類法線貼圖

實施
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) { 

      } 
     } 

    } 

} 

結果:

result 1

result 2

result 3

希望它能幫助謝謝。 :)

+1

偉大的答案。謝謝 – Ravi 2014-11-03 07:30:36

+0

偉大的例子,一個更正,但最終LatLng位置=新LatLng(lon,緯度);應該是最終的LatLng position = new LatLng(lat,lon); – prasunnair 2016-04-16 08:31:20

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

是的,我看到了答案,但它並沒有解決我的問題。我用我用過的解決方案編輯了我的問題,但這種方案並不奏效。 – 2014-11-03 06:25:49