2016-07-27 258 views
2

我想在ScrollView內使用Google Map,但它看起來比我想像的更難。我第一次創建所有內容並在平板電腦上運行應用程序。在平板電腦上,用戶界面看起來不錯,地圖非常完美。當我嘗試使用更小的設備時,我意識到地圖的80%大小是不可見的。我可以向下滾動直到屏幕結束,但是有一個很大的空位而不是地圖的其餘部分。在SrcollView內顯示完整映射android

檢查下面的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:id="@+id/scrollView"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" 
    android:id="@+id/relativeinfo1"> 

<View 
    android:layout_width="wrap_content" 
    android:layout_height="35dp" 
    android:layout_above="@+id/titleinfo" 
    android:layout_toRightOf="@+id/imageinfo"/> 


<TextView 
    android:id="@+id/titleinfo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:textSize="20dp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:layout_toRightOf="@+id/imageinfo" 
    android:layout_marginLeft="20dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="phone" 
    android:id="@+id/mooobil"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="phone" 
    android:id="@+id/telefoneeo"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="email" 
    android:id="@+id/emailese"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="web" 
    android:id="@+id/honlapese"/> 


<ImageView 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:id="@+id/imageinfo" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp"/> 

<View 
    android:layout_width="wrap_content" 
    android:layout_height="15dp" 
    android:id="@+id/view1" 
    android:layout_below="@+id/imageinfo"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/cimtext" 
    android:textStyle="bold" 
    android:text="Cím: " 
    android:textSize="15dp" 
    android:textColor="#000000" 
    android:visibility="invisible"/> 


<View 
    android:layout_width="wrap_content" 
    android:layout_height="10dp" 
    android:id="@+id/vonal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:background="#FFD700" /> 

<View 
    android:layout_width="wrap_content" 
    android:layout_height="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:id="@+id/vonal2" 
    android:background="#FFD700"/> 



<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    android:id="@+id/relativemaplayout" 
    android:clickable="true" 
    android:background="#000000"> 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     class="com.google.android.gms.maps.MapFragment" /> 


</RelativeLayout> 
</RelativeLayout> 

這裏檢查Java代碼:

private ScrollView mScrollView; 

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_service_info,parent, false); 
    mScrollView = (ScrollView) v.findViewById(R.id.scrollView); 
    //transparentImageView = (ImageView) v.findViewById(R.id.transparent_image); 
    mScrollView.requestDisallowInterceptTouchEvent(true); 

    ///// MAP ///// 
    double glat = 0; 
    double glong = 0; 


    map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap(); 

    if((si.getGpslat().length()) != 0 && (si.getGpslong().length() != 0)){ 
     glat = Double.parseDouble(si.getGpslat()); 
     glong = Double.parseDouble(si.getGpslong()); 

     final LatLng CEL = new LatLng(glat,glong); 

     if (map!=null){ 
      MapsInitializer.initialize(getActivity()); 
      map.addMarker(new MarkerOptions().position(CEL)); 
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(CEL ,18)); 
      map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null); 
     } 

    }else{ 
     relmap.setVisibility(View.GONE); 
    } 


    transparentImageView.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getAction(); 
      Log.d("action","down"); 
      switch (action) { 
       case MotionEvent.ACTION_DOWN: 
        // Disallow ScrollView to intercept touch events. 
        mScrollView.requestDisallowInterceptTouchEvent(true); 
        Log.d("action","down"); 
        return false; 

       case MotionEvent.ACTION_UP: 
        // Allow ScrollView to intercept touch events. 
        mScrollView.requestDisallowInterceptTouchEvent(false); 
        Log.d("action","up"); 
        return true; 

       case MotionEvent.ACTION_MOVE: 
        mScrollView.requestDisallowInterceptTouchEvent(true); 
        Log.d("action","move"); 
        return false; 

       default: 
        return true; 
      } 
     } 
    }); 

    transparentImageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getActivity(), "anyád", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return v; 
} 

是否有可能顯示一個SrcollView全地圖,而使用平板電腦?

這是它是什麼樣子:

enter image description here

+0

您可以發佈一個屏幕短,讓我看看它的外觀如何 –

+0

可以添加屏幕快照 – DKV

+0

是的,我添加了一個屏幕截圖。我希望你能幫助我 –

回答

0

這是解決方案:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:id="@+id/scrollView"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" 
    android:id="@+id/relativeinfo1"> 


<View 
    android:layout_width="wrap_content" 
    android:layout_height="35dp" 
    android:layout_above="@+id/titleinfo" 
    android:layout_toRightOf="@+id/imageinfo"/> 


<TextView 
    android:id="@+id/titleinfo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:textSize="20dp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:layout_toRightOf="@+id/imageinfo" 
    android:layout_marginLeft="20dp"/> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="phone" 
     android:id="@+id/cimtext"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="phone" 
    android:id="@+id/mooobil"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="phone" 
    android:id="@+id/telefoneeo"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="email" 
    android:id="@+id/emailese"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:autoLink="web" 
    android:id="@+id/honlapese"/> 


<ImageView 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:id="@+id/imageinfo" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp"/> 

<View 
    android:layout_width="wrap_content" 
    android:layout_height="15dp" 
    android:id="@+id/view1" 
    android:layout_below="@+id/imageinfo"/> 


<View 
    android:layout_width="wrap_content" 
    android:layout_height="10dp" 
    android:id="@+id/vonal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:background="#FFD700" /> 

<View 
    android:layout_width="wrap_content" 
    android:layout_height="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:id="@+id/vonal2" 
    android:background="#FFD700"/> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="400dp" 
    tools:context=".MainActivity" 
    android:id="@+id/relativemaplayout" 
    android:clickable="true"> 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="wrap_content" 
     android:layout_height="400dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="5dp" 
     class="com.google.android.gms.maps.MapFragment" /> 

</RelativeLayout> 


    <ImageView 
     android:id="@+id/transparent_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@color/transparent" 
     android:layout_alignTop="@+id/relativemaplayout" 
     android:layout_alignBottom="@+id/relativemaplayout" 
     android:layout_alignEnd="@+id/relativemaplayout" 
     android:layout_alignRight="@+id/relativemaplayout" 
     android:layout_alignLeft="@+id/relativemaplayout" 
     android:layout_alignStart="@+id/relativemaplayout"/> 
</RelativeLayout> 

而你需要把它添加到片段:

transparentImageView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getAction(); 
      Log.d("action","down"); 
      switch (action) { 
       case MotionEvent.ACTION_DOWN: 
        // Disallow ScrollView to intercept touch events. 
        mScrollView.requestDisallowInterceptTouchEvent(true); 
        Log.d("action","down"); 
        return false; 

       case MotionEvent.ACTION_UP: 
        // Allow ScrollView to intercept touch events. 
        mScrollView.requestDisallowInterceptTouchEvent(false); 
        Log.d("action","up"); 
        return true; 

       case MotionEvent.ACTION_MOVE: 
        mScrollView.requestDisallowInterceptTouchEvent(true); 
        Log.d("action","move"); 
        return false; 

       default: 
        return true; 
      } 
     } 
    }); 
0

我覺得它`因爲你layout_height設置爲fragment.That造成一定的測量誤差。

嘗試將layout_height設置爲某個dp。

如:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="300dp" 
tools:context=".MainActivity" 
android:id="@+id/relativemaplayout" 
android:clickable="true" 
android:background="#000000"> 

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    class="com.google.android.gms.maps.MapFragment" /> 

+0

不幸的是,這不是一個好的解決方案。 –

1

嘗試這樣

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff0000" 
    android:id="@+id/relativeinfo1"> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_above="@+id/titleinfo" 
     android:layout_toRightOf="@+id/imageinfo"/> 


    <TextView 
     android:id="@+id/titleinfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:textColor="#000000" 
     android:layout_toRightOf="@+id/imageinfo" 
     android:layout_marginLeft="20dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="phone" 
     android:id="@+id/mooobil"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="phone" 
     android:id="@+id/telefoneeo"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="email" 
     android:id="@+id/emailese"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="web" 
     android:id="@+id/honlapese"/> 


    <ImageView 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:id="@+id/imageinfo" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp"/> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="15dp" 
     android:id="@+id/view1" 
     android:layout_below="@+id/imageinfo"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cimtext" 
     android:textStyle="bold" 
     android:text="Cím: " 
     android:textSize="15dp" 
     android:textColor="#000000" 
     android:visibility="invisible"/> 


    <View 
     android:layout_width="wrap_content" 
     android:layout_height="10dp" 
     android:id="@+id/vonal" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#FFD700" /> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:id="@+id/vonal2" 
     android:background="#FFD700"/> 


    <RelativeLayout 
     android:layout_alignParentBottom="true" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:context=".MainActivity" 
     android:id="@+id/relativemaplayout" 
     android:clickable="true" 
     android:background="#000000"> 

     <fragment 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      class="com.google.android.gms.maps.MapFragment" /> 


    </RelativeLayout> 
</RelativeLayout> 

更新

如果你仍然有概率LEM請參考這一點,並嘗試

Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically

+0

現在我無法向下滾動,因此我只能看到地圖的頂部。所以地圖的大小和以前一樣。 –

+0

嘗試將片段寬度更改爲 android:layout_height =「match_content」 –

+0

讓我知道它的工作 –