简体   繁体   English

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

[英]Floating action button on top of map fragment

I want to Place floating action button on top of map fragment. 我想在地图片段顶部放置浮动操作按钮。 My map fragment is completely filled the entire screen. 我的地图片段完全填满整个屏幕。 When I place a floating action button it is not coming on top of map fragment Please refer the image attached: 当我放置一个浮动动作按钮时它不会出现在地图片段的顶部请参考附图:

带搜索栏的地图片段

Please find layout code here 请在此处找到布局代码

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

You can use FrameLayout to acieve what you want. 您可以使用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>

Hi you can try to this code.. 嗨,你可以尝试这个代码..

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

You just need to change the android:layout_gravity="top|right" 你只需要改变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>

I had a similar requirement, I used android material floating action button. 我有类似的要求,我使用了android材质浮动动作按钮。

The button came with material design. 按钮带有材料设计。

The trick was to add the button inside the fragment and then change the margin gravity accordingly. 诀窍是在片段内添加按钮,然后相应地更改边缘重力。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM