0

je travaillais sur un projet et en suivant ces instructionsAjout d'une vue personnalisée (mise en page linéaire) au sommet d'une carte de soutien Fragment est un échec

(How to add buttons at top of map fragment API v2 layout)

Je parvins sur l'ajout de composants sur la en haut du fragment de carte (pas le fragment de carte de support). Après cela, je fait un changement à mon code et en suivant ces étapes

(SupportMapFragment Instead of MapFragment)

je changé ma carte Fragment à l'appui de carte Fragment. Je ne change rien de ma carte xml, en plus

class="com.google.android.gms.maps.MapFragment" 

à

class="com.google.android.gms.maps.SupportMapFragment" 

mais quand je courais mon application ma disposition linéaire ne montre pas sur la carte plus. J'ai essayé de changer de FrameLayout à RelativeLayout et d'ajouter quelques propriétés, mais il semble qu'il se chevauche encore mes LinearLayout.

Y at-il quelque chose qui manque ou au mauvais endroit?

PS. Dans la vue Mise en page graphique XML, je peux voir que la disposition linéaire est correctement positionnée sur la carte.

XML Mise en page graphique: http://j52.imgup.net/Imagen156161.png
Screencap de l'application en cours d'exécution: http://i87.imgup.net/Imagen171ef6.png

Voici mon MapFragment.xml actuel

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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:orientation="vertical" 
    > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

    <LinearLayout 
     android:id="@+id/box" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:layout_margin="10dp" 
     android:background="@drawable/button_noborder_background" 
     android:orientation="vertical" > 

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

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="vertical" 
       android:paddingBottom="5dp" 
       android:paddingLeft="5dp" 
       android:paddingTop="5dp" > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:orientation="horizontal" > 

        <ImageView 
         android:layout_width="25dp" 
         android:layout_height="25dp" 
         android:padding="2dp" 
         android:src="@drawable/icon_marker_inactive" /> 

        <TextView 
         android:id="@+id/txtOrigen" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:background="@color/white" 
         android:gravity="center_vertical" 
         android:hint="Origen" 
         android:padding="2dp" 
         android:textAppearance="@android:style/TextAppearance.Small" > 
        </TextView> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:orientation="horizontal" > 

        <ImageView 
         android:layout_width="25dp" 
         android:layout_height="25dp" 
         android:padding="2dp" 
         android:src="@drawable/icon_marker_inactive" /> 

        <TextView 
         android:id="@+id/txtDestino" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:background="@color/white" 
         android:gravity="center_vertical" 
         android:hint="Destino" 
         android:padding="2dp" 
         android:textAppearance="@android:style/TextAppearance.Small" > 
        </TextView> 
       </LinearLayout> 
      </LinearLayout> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:background="@drawable/button_blue_background" 
       android:gravity="center" 
       android:paddingLeft="15dp" 
       android:paddingRight="15dp" 
       android:text="Ruta" 
       android:textAppearance="@android:style/TextAppearance.Small" 
       android:textColor="@color/white" > 
      </TextView> 
     </LinearLayout> 
    </LinearLayout> 

</FrameLayout> 

activity_main.xml: Mes fragments contenant XML

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" > 
 

 
    <!-- 
 
     As the main content view, the view below consumes the entire 
 
     space available using match_parent in both dimensions. 
 
    --> 
 

 
    <FrameLayout 
 
     android:id="@+id/container" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" /> 
 

 
    <!-- 
 
     android:layout_gravity="start" tells DrawerLayout to treat 
 
     this as a sliding drawer on the left side for left-to-right 
 
     languages and on the right side for right-to-left languages. 
 
     If you're not building against API 17 or higher, use 
 
     android:layout_gravity="left" instead. 
 
    --> 
 
    <!-- 
 
     The drawer is given a fixed width in dp and extends the full height of 
 
     the container. 
 
    --> 
 

 
    <fragment 
 
     android:id="@+id/navigation_drawer" 
 
     android:layout_width="@dimen/navigation_drawer_width" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     tools:layout="@layout/fragment_navigation_drawer" /> 
 

 
</android.support.v4.widget.DrawerLayout>

De MainActivity.java, la méthode onNavigationDrawerItemSelected où je crée MapFragment

@Override 
    public void onNavigationDrawerItemSelected(int position) { 
     // update the main content by replacing fragments 
     Fragment fragment; 
     FragmentManager fragmentManager; 
     switch (position) { 
     default: 
     case 0: 
      fragment = MapFragment.newInstance(position + 1); 
      onSectionAttached(position + 1); 
      break; 
     } 
     fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.container, fragment) 
       .commit(); 

    } 

MapFragment.java

import android.app.Activity; 
    import android.content.Context; 
    import android.location.LocationManager; 
    import android.os.Build; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentManager; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.SupportMapFragment; 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public class MapFragment extends SupportMapFragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 
     private static View rootView; 
     LocationManager locationManager; 
     static GoogleMap map; 

     /** 
     * Returns a new instance of this fragment for the given section number. 
     */ 
     public static SupportMapFragment newInstance(int sectionNumber) { 
      SupportMapFragment fragment = new SupportMapFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public MapFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      if (rootView != null) { 
       ViewGroup parent = (ViewGroup) rootView.getParent(); 
       if (parent != null) 
        parent.removeView(rootView); 
      } 

      try { 
       rootView = inflater.inflate(R.layout.fragment_mapa, container, 
         false); 
      }catch(Exception e){ 

      } 

      // Location Service 
      locationManager = (LocationManager) getActivity().getSystemService(
        Context.LOCATION_SERVICE); 

      // Inicializacion del mapa 
      map = getMapFragment().getMap(); 
      map.setMyLocationEnabled(true); 
      map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

      return rootView; 
     } 

     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      ((MainActivity) activity).onSectionAttached(getArguments().getInt(
        ARG_SECTION_NUMBER)); 
     } 
     private SupportMapFragment getMapFragment() { 
      FragmentManager fm = null; 

      // Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT); 
      // Log.d(TAG, "release: " + Build.VERSION.RELEASE); 

      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
       // Log.d(TAG, "using getFragmentManager"); 
       fm = getFragmentManager(); 
      } else { 
       // Log.d(TAG, "using getChildFragmentManager"); 
       fm = getChildFragmentManager(); 
      } 

      return (SupportMapFragment) fm.findFragmentById(R.id.map); 
     } 
    } 

Répondre

0

dans votre fichier xml

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

devrait être

<FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

parce que vous initiez dans le tiroir du navigateur:

fragmentManager.beginTransaction().replace(R.id.container, fragment) 
       .commit(); 

mais il n'y a pas id = le récipient.

+0

Désolé , vient d'éditer mon post en ajoutant le conteneur XML de mes fragments (activity_main.xml). Cette ligne remplace mon conteneur d'activité principal par mon fragment de carte: fragmentManager.beginTransaction(). Replace (R.id.container, fragment) .commit(); – Yops

1

Dans votre activité, gardez une référence à votre disposition linéaire

LinearLayout root = (LinearLayout) findViewById(R.id.root); 

Puis, après le fragment est ajouté à la mise en page avec transaction fragment, apportez votre disposition linéaire à l'avant en faisant

root.bringToFront(); 
+0

Dans ma méthode onCreateView Je viens d'ajouter cette ligne, mais cela n'a pas fonctionné :( boîte LinearLayout = (LinearLayout) rootView.findViewById (R.id.box); box.bringToFront(); Quand j'utilisais MapFragment Au lieu de SupportMapFragment, cela a bien fonctionné, mais je n'ai pas trouvé de problèmes similaires dans les autres messages jusqu'à présent. – Yops