在Google地图上显示标签

在Google地图上显示标签

问题描述:

我在显示我的Google地图上的TextView时遇到问题。我在onCreate方法中以编程方式在FrameLayout中添加MapFragment,并且TextView位于XML文件中。对于我所看到的,TextView已正确显示,但MapFragment会将其添加到FrameLayout中并隐藏TextView。我已经在这里搜索了StackOverflow,但是我可以找到的每个类似的主题都是使用XML中的静态MapFragment。在Google地图上显示标签

这里是我的代码:SRC/JAVA/SearchMapFragment.java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    ... 
    mMapFragment = MapFragment.newInstance(options); 
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.map_layout,mMapFragment); 
    fragmentTransaction.commit(); 
    ((TextView)rootView.findViewById(R.id.map_label)).setVisibility(View.VISIBLE); 
    ... 
} 

和我的XML FIL:RES /布局/ fragment_search_map.xml

<LinearLayout 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" 
    android:id="@+id/search_map_wrapper_layout" 
    tools:context="com.appsolute.ParkYoo.SearchMapFragment"> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/map_layout" 
     android:layout_weight="100"> 

     <TextView style="@style/DarkBlueText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:id="@+id/map_label" 
      android:gravity="center" 
      android:layout_marginTop="15dp" 
      android:layout_marginLeft="15dp" 
      android:layout_marginRight="15dp" 
      android:visibility="gone"/> 

    </FrameLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="1"> 

     <FrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/search_map_frame_layout"> 

      <Button style="@style/WhiteText" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Lancer la recherche" 
       android:layout_margin="5dp" 
       android:id="@+id/launch_search" 
       android:background="@drawable/lancer" /> 

     </FrameLayout> 
    </LinearLayout> 
</LinearLayout> 

任何帮助将不胜感激感谢中前进!

+0

有你尝试在地图上创建动态覆盖的TextView? – Mohit 2014-10-01 13:13:56

+0

如果我知道如何去做,我会做到的。你是否建议以编程方式添加TextView? – user3476114 2014-10-01 13:46:23

+0

你是否尝试用RelativeLayout代替LinearLayout?你可以在上面放置你的TextView。 – 2014-10-01 13:46:34

最后我找到了答案。基本上我记得在android上布局文件的xml架构是“回到前面”的地方。这意味着xml布局文件越低,widget的前端就越多。所以这些小部件是一个在另一个之上。

于是,我就在xml文件中放置一个TextView的FrameLayout里之后,但它竟然是一样的,可能是因为是在的FrameLayout在运行时添加的片段所以它是最后一个加入重叠在充气的TextView onCreate方法。

为了解决我的问题,我已经返工我这样的XML:

<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" 
    android:orientation="vertical" 
    android:id="@+id/search_map_wrapper_layout" 
    android:gravity="" 
    tools:context="com.appsolute.ParkYoo.SearchMapFragment"> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/map_layout_leave_place" 
     android:layout_above="@+id/utility_linear_layout"> 

    </FrameLayout> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:id="@+id/leave_place_label"> 

    </FrameLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/utility_linear_layout" 
     android:orientation="vertical" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentBottom="true"> 

     <TextView style="@style/DarkBlueText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:id="@+id/street_leave_parking_spot" 
      android:layout_marginTop="10dp" 
      android:text="Rue X"/> 

     <TextView style="@style/TitleText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:id="@+id/city_leave_parking_spot" 
      android:paddingBottom="15dp" 
      android:background="@drawable/discontinued_line" 
      android:text="75009 PARIS"/> 

     <Button style="@style/WhiteText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:text="Mémoriser ma place" 
      android:id="@+id/remember_parking_spot" 
      android:background="@drawable/lancer" 
      android:layout_gravity="center_horizontal|bottom"/> 

     </LinearLayout> 

</RelativeLayout> 

第一的FrameLayout(map_layout_leave_place)是最深的,是在我的地图就会显示出来。第二个FrameLayout(leave_place_label)是我的TextView将被放置在谷歌地图布局上的位置,目前为空。其余的在运行时发生。

我创建了一个LabelFragment,其中包含仅在运行时使用常规片段事务添加的TextView,当我单击将由leave_place_label托管的按钮时。

LabelFragment frag = LabelFragment.newInstance(); 
FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.add(R.id.leave_place_label,frag); 
ft.commit(); 

这里是激起它的作用:

https://drive.google.com/file/d/0B1KRFmgxHcLtMEY3dDQ1c0NRYWM/edit?usp=sharing