1

我想在地图片段顶部放置浮动动作按钮。 我的地图片段完全填满整个屏幕。当我把一个浮动的操作按钮时不会在地图片段的顶部到来 请参见附件图片:地图片段顶部的浮动动作按钮

map fragment with search bar

这里请找布局代码

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillViewport="true"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     android:orientation="vertical" 
     android:weightSum="1"> 


     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/margin_small" 
      > 

      <fragment 
       android:id="@+id/autocomplete_fragment" 
       android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 
      <!--<ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@drawable/bell32" 
       android:visibility="invisible" 
       /> 
      <android.support.v7.widget.SwitchCompat 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/alarm_on_switch" 
       android:layout_gravity="right|top" 
       android:layout_marginRight="@dimen/margin_small" 
       android:drawableLeft="@drawable/bell32" 
       />--> 

     </android.support.v7.widget.CardView> 
     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      tools:context="reminder.locrem.com.locationreminder.MapsActivity" 
      android:layout_height="match_parent" /> 

     <android.support.design.widget.FloatingActionButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top|right" 
      android:layout_margin="16dp" 
      android:src="@drawable/done" 
      app:layout_anchorGravity="bottom|right|end" 
      app:elevation="4dp" /> 

    </LinearLayout> 

</ScrollView> 
+0

发表您的XML代码 –

+0

我已经编辑了问题补充XML代码 – sasikanth

回答

3

你可以使用FrameLayout来获得你想要的。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="1"> 
     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/margin_small"> 
      <fragment 
       android:id="@+id/autocomplete_fragment" 
       android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

     </android.support.v7.widget.CardView> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <fragment 
       android:id="@+id/map" 
       android:name="com.google.android.gms.maps.SupportMapFragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context="reminder.locrem.com.locationreminder.MapsActivity" /> 

      <android.support.design.widget.FloatingActionButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="top|right" 
       android:layout_margin="16dp" 
       android:src="@drawable/done" 
       app:elevation="4dp" 
       app:layout_anchorGravity="bottom|right|end" /> 
     </FrameLayout> 

    </LinearLayout> 
</ScrollView> 
+0

感谢您的帮助 – sasikanth

+0

@sasikanth善意upvote,如果这可以解决您的问题。 –

1

你好你可以试试这个代码..

<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"> 

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


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/imageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_margin="30dp" 
    android:background="@android:color/transparent" 
    android:clickable="true" 
    android:onClick="myLocationCall" 
    android:rotation="0" /> 

    </RelativeLayout> 
+0

嗨sasikanth如果这能帮助你,那么请接受并给予好评this..thanks –

+0

嗨感谢您的建议,但它并没有帮助我张贴我的布局xml代码以及请让我知道如果有任何错误 – sasikanth

0

你只需要改变机器人:layout_gravity =“顶|里ght“

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

      <com.google.android.gms.maps.MapView 
      android:id="@+id/mapView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      /> 

      <android.support.design.widget.FloatingActionButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="top|right" 
       android:layout_margin="16dp" 
       android:src="@drawable/ic_done" 
       app:layout_anchor="@id/lvToDoList" 
       app:layout_anchorGravity="bottom|right|end" /> 
</android.support.design.widget.CoordinatorLayout>