0

我工作的一个项目,并按照下列指示添加自定义视图(线性布局),以支援地图片段的顶部失败

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

我上的添加成分成功地图片段的顶部(不是支持地图片段)。在那之后,我做了一个改变我的代码,并按照以下步骤

SupportMapFragment Instead of MapFragment

我改变了我的地图片段支持地图片段。我没有任何东西从我的地图XML改变,除了

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

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

,但是当我跑我的应用我的线性布局不会在地图上显示了。 我试图从FrameLayout改为RelativeLayout并添加一些属性,但它似乎仍然与我的LinearLayout重叠。

是否有遗漏或错误的地方?

PS。在XML图形布局视图中,我可以看到线性布局在地图上正确定位。

XML图形布局:http://j52.imgup.net/Imagen156161.png
撷取画面运行的应用程序:http://i87.imgup.net/Imagen171ef6.png

这是我目前的MapFragment.xml

<?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中:我的片段容器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>

从MainActivity.java,onNavigationDrawerItemSelected方法,其中创建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); 
     } 
    } 

回答

0

进入XML文件

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

应该

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

,因为你开始进入导航抽屉:

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

但没有ID =集装箱。

+0

对不起把你的线性布局前,只是编辑我的帖子,添加我的片段的容器XML(activity_main.xml)。这行代替了我的主要活动容器和我的地图片段:fragmentManager.beginTransaction()。replace(R.id.container,fragment) .commit(); – Yops

1

在您的活动,保持对它的引用您的线性布局

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

然后将片段添加到与片段交易的布局后,通过做

root.bringToFront(); 
+0

在我onCreateView方法我只是说这条线,但它没有工作:( 的LinearLayout盒=(LinearLayout中)rootView.findViewById(R.id.box); box.bringToFront(); 当我使用MapFragment而不是SupportMapFragment它工作得很好,但我到目前为止还没有在其他帖子中发现任何类似的问题。 – Yops

相关问题