簡體   English   中英

在地圖片段頂部的浮動行動按鈕

[英]Floating action button on top of map fragment

我想在地圖片段頂部放置浮動操作按鈕。 我的地圖片段完全填滿整個屏幕。 當我放置一個浮動動作按鈕時它不會出現在地圖片段的頂部請參考附圖:

帶搜索欄的地圖片段

請在此處找到布局代碼

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

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

嗨,你可以嘗試這個代碼..

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

你只需要改變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>

我有類似的要求,我使用了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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM