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>