2

Je travaille sur un projet Android en ce moment: ce que je veux faire est d'avoir la moitié supérieure de l'écran pour la carte et l'autre moitié pour des informations que j'écrirais plus tard. Mon problème est que la carte prend tout l'écran, j'ai essayé différentes dispositions (Relative, Grid) et gonfler une autre disposition mais la carte prend toujours tout l'écran que j'ai utilisé.Redimensionner 'Google maps V2' pour prendre la moitié de l'écran au lieu de l'écran entier

voici mon code:

public class Map extends SherlockMapFragment implements IPage, View.OnClickListener { 

private GoogleMap googleMap; 
private Marker  marker; 
ViewGroup   view; 


public Map() { 
    super(); 
} 
    @Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    view = (ViewGroup) inflater.inflate(R.layout.map_page, null); 

    googleMap = getMap(); 
    googleMap.setMyLocationEnabled(true); 
    marker = googleMap.addMarker(new MarkerOptions().title("Event").position(new LatLng(0, 0))); 

    final LatLng latLng = new LatLng(48.8146, 2.39525);  

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f)); 
    marker.setPosition(latLng); 

    return view; 
} 

et xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

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

    <fragment 
    android:id="@+id/fragment_map" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 
</LinearLayout> 

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

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/infotext">   
    </TextView> 

</LinearLayout> 
</LinearLayout> 

Encore une fois, la carte fonctionne sans problème, mais il faut tout l'écran, je ne comprends vraiment pas pourquoi. En outre, j'utilise le lib SherlockBar (ce qui me cause quelques problèmes pour implémenter la carte).

J'apprécierais grandement toute sorte d'aide, je suis encore un peu nouveau avec le développement sur Java/Eclipse, donc les erreurs se produisent souvent. Merci!

+0

Pouvez-vous poster le code de l'activité dans laquelle vous appelez le fragment? –

Répondre

9

Si vous voulez avoir votre écran partagé en deux égalent parties, il vous suffit d'utiliser la mise en page suivante:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/fragment_map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="50" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="50" 
     android:id="@+id/infotext" />   

</LinearLayout> 

Comme vous avez seulement deux points de vue, vous ne devez pas les envelopper dans d'autres LinearLayout s.

Définir le même poids pour le Fragment et le TextView est la clé. Il donnera la moitié de l'écran (en hauteur que l'orientation du LinearLayout est vertical) pour chaque View.

+0

Merci de votre aide, j'ai oublié de mentionner que j'ai déjà essayé d'utiliser "layout_weight" avant et cela n'a pas aidé T_T Je viens de modifier mon xml avec ce que vous avez écrit mais la carte prend tout l'écran – LostCid

0

Utilisez celui-ci: -

import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 
    import com.google.android.gms.maps.CameraUpdate; 
    import com.google.android.gms.maps.CameraUpdateFactory; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.MapView; 
    import com.google.android.gms.maps.MapsInitializer; 
    import com.google.android.gms.maps.model.LatLng; 

    import android.annotation.SuppressLint; 
    //import android.app.Fragment; 
    import android.support.v4.app.Fragment; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    @SuppressLint("NewApi") 
    public class Map_Check extends Fragment { 

     public Map_Check(){} 


     MapView mapView; 
     GoogleMap map; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.map_view, container, false); 

      // Gets the MapView from the XML layout and creates it 
      mapView = (MapView) v.findViewById(R.id.mapview); 
      mapView.onCreate(savedInstanceState); 

      // Gets to GoogleMap from the MapView and does initialization stuff 
      map = mapView.getMap(); 
      map.getUiSettings().setMyLocationButtonEnabled(false); 
      map.setMyLocationEnabled(true); 

      MapsInitializer.initialize(this.getActivity()); 

      // Updates the location and zoom of the MapView 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(48.8146, 2.39525), 10); 
      map.animateCamera(cameraUpdate); 

      return v; 
     } 

     @Override 
     public void onResume() { 
      mapView.onResume(); 
      super.onResume(); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      mapView.onDestroy(); 
     } 

     @Override 
     public void onLowMemory() { 
      super.onLowMemory(); 
      mapView.onLowMemory(); 
     } 

    } 

Et map_view.xml: -

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="150dp"> 
<com.google.android.gms.maps.MapView 
    android:id="@+id/mapview" 
    android:layout_width="100dp" 
    android:layout_height="100dp"/> 
</LinearLayout> 
+0

merci pour votre aide , J'ai essayé ce code mais toujours la carte prend tout l'écran:/ – LostCid

+0

@LostCid, spécifiez la taille de layout_width et layout_height dans MapView dans map_view.xml .... (Si cela ne fonctionne pas, spécifiez la taille dans LinearLayout). Cela fonctionnera sûrement, mais la taille devrait être en fonction de votre espace ... – Laxmeena

1

Je sais qu'il est un peu tard, mais depuis que je suis passé à chercher réponse à ma question, voici mes pensées:

Il semble que votre disposition linéaire qui contient MapFragment occupe tout l'écran. Au lieu d'avoir match_parent, modifiez android: layout_height à fill_parent.

La différence ici est que match_parent rend la hauteur de la vue pour prendre ALL HEIGHT du parent. Dans votre cas parent est la disposition linéaire qui est de la taille de l'écran.

fill_parent remplit réellement l'espace libre du parent.

Pour votre exemple le code XML suivant devrait fonctionner:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <fragment 
    android:id="@+id/fragment_map" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1.0" 
    android:orientation="vertical" 
    android:weight="7"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/infotext">   
    </TextView> 

    </LinearLayout> 
</LinearLayout> 

Avoir wrap_content avec des poids vous permet de faire layouting relative.Sur mes appareils, les poids suivants ressemblent à ça (Et vous pouvez jouer avec ceux sur votre pour atteindre les tailles désirées)

|----------------| 
|    | 
|    | 
|    | 
|    | 
|  Map  | 
|    | 
|    | 
|    | 
|    | 
|----------------| 
|  Text  | 
|----------------| 
+0

'match_parent' et' fill_parent' sont exactement les mêmes dans tous les sens. 'fill_parent' a juste été renommé' match_parent' pour décrire plus précisément ce qu'il fait. Comme vous pouvez le voir ici, les deux sont égaux à -1 http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT (d'où le downvote) – zundi

+0

Merci, ma source d'information était une classe dans l'université à laquelle j'ai assisté et apparemment mon professeur avait tort. C'est bon de trouver quelque chose de nouveau une fois de temps en temps. –