在地图片段顶部的浮动行动按钮

时间:2016-12-17 10:33:10

标签: android google-maps android-fragments floating-action-button

我想在地图片段顶部放置浮动操作按钮。 我的地图片段完全填满整个屏幕。当我放置一个浮动动作按钮时,它不会出现在地图片段的顶部 请参考附图:

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>

4 个答案:

答案 0 :(得分:7)

您可以使用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>

答案 1 :(得分:3)

您好,您可以尝试使用此代码..

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

答案 2 :(得分:0)

你只需要改变 android:layout_gravity =“top | right”

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

答案 3 :(得分:0)

我有类似的要求,我使用了android材质的浮动操作按钮。

按钮带有材料设计。

诀窍是在片段内添加按钮,然后相应地更改页边距重力。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            tools:context=".ui.Maps.MapsActivity" >

            <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:layout_gravity="end|bottom"
            android:layout_margin="8dp"
            android:src="@android:drawable/ic_menu_revert" />
        </fragment>
相关问题